Get first n characters of a String in Java
This post will discuss how to get the first n
characters of a String in Java, where n
is a non-negative integer.
1. Using substring()
method
To get the first n
characters of a String, we can pass the starting index as 0 and ending index as n
to the substring()
method. Note that the substring()
method returns IndexOutOfBoundsException
if the ending index is larger than the length of the String. This can be handled using the ternary operator, as demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class Main { public static String firstNChars(String str, int n) { if (str == null) { return null; } return str.length() < n ? str : str.substring(0, n); } public static void main(String[] args) { String str = "Techie Delight"; int n = 6; String truncated = firstNChars(str, n); System.out.println(truncated); // Techie } } |
Instead of the ternary operator, we can also pass the minimum of string’s length and n
as ending index in the substring()
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Main { public static String firstNChars(String str, int n) { if (str == null) { return null; } return str.substring(0, Math.min(str.length(), n)); } public static void main(String[] args) { String str = "Techie Delight"; int n = 6; String truncated = firstNChars(str, n); System.out.println(truncated); // Techie } } |
2. Using Apache Commons Lang
Apache Commons Lang library StringUtils
class offers several utility methods for null-safe substring extractions. To get the first n
characters of a String, we can use the overload version of the substring() method, which returns a substring from a specified start position to a specified end position. Note that there is no need to explicitly handle the exception if n
is more than the string’s length.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.apache.commons.lang3.StringUtils; class Main { public static void main(String[] args) { String str = "Techie Delight"; int n = 6; String truncated = StringUtils.substring(str, 0, n); System.out.println(truncated); // Techie } } |
The StringUtils
class also has the left() method specifically for extracting the leftmost n
characters of a String. If the string is null or n
characters are not available, it returns the original String without any exception.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.apache.commons.lang3.StringUtils; class Main { public static void main(String[] args) { String str = "Techie Delight"; int n = 6; String truncated = StringUtils.left(str, n); System.out.println(truncated); // Techie } } |
3. Using Guava
Alternatively, we can use Guava’s Ascii.truncate() method to truncate the given string to the specific length.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import com.google.common.base.Ascii; class Main { public static void main(String[] args) { String str = "Techie Delight"; int n = 6; String truncated = Ascii.truncate(str, n, ""); System.out.println(truncated); // Techie } } |
4. Using String.format()
method
Finally, we can simply format the string using the String.format()
method, which takes width indicating the minimum number of characters to be written to the output.
1 2 3 4 5 6 7 8 9 10 11 |
class Main { public static void main(String[] args) { String str = "Techie Delight"; int n = 6; String truncated = String.format("%."+ n +"s", str); System.out.println(truncated); // Techie } } |
That’s all about getting first n
characters of a String in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)