In the previous tutorial, you learned some ways to initialize a String object in Java as well as the differences between them, and you also learned how String implements the Immutable concept. In this tutorial, I will continue to present to you all the basic methods of the String object.
Table of Contents
charAt() method
Used to get a character at the index in the String.
Remember that String is stored in an array variable, so the index will start at zero.
For example:
1 2 3 4 5 6 7 8 9 10 |
package com.huongdanjava.javaexample; public class Example { public static void main(String[] args) { String a = "Khanh"; char ch = a.charAt(2); System.out.println(ch); } } |
Result:
In this example, the character at index = 0 will be “K”, 1 will be “h”, and so at index = 2 will be “a”.
indexOf() method
Used to search for a character or a string in the String.
This method returns the index that starts the string or character.
This method has many different overload methods, which help us to search for a character, a string, a character starting with a certain index, or a string starting with a certain index.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.huongdanjava.javaexample; public class Example { public static void main(String[] args) { String a = "Khanh"; int i = a.indexOf("h"); System.out.println(i); int j = a.indexOf("an"); System.out.println(j); int k = a.indexOf("h", 2); System.out.println(k); } } |
Result:
substring() method
This method is used to get a string in any string starting at a specified index until the end of the string or starting from one index to another in any String.
This method also has an overload method.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.javaexample; public class Example { public static void main(String[] args) { String a = "Khanh"; String b = a.substring(2); System.out.println(b); String c = a.substring(1, 2); System.out.println(c); } } |
Result:
You should note that the substring() method from one index to the other does not return the character at the other index. In my example, although I call substring 1 to 2, the character at index 2 (“a”) has not been returned.
trim() method
If your strings contain more than one blank or double-spaced character or tab, and now you want to delete them, use the trim() method.
For example:
1 2 3 4 5 6 7 8 9 10 11 |
package com.huongdanjava.javaexample; public class Example { public static void main(String[] args) { String a = " Khanh "; String b = a.trim(); System.out.println("-" + b + "-"); } } |
Result:
replace() method
Used to create another new String object, by replacing all characters or strings in any string with the character or string passed in.
For example:
1 2 3 4 5 6 7 8 9 10 11 |
package com.huongdanjava.javaexample; public class Example { public static void main(String[] args) { String a = " Khanh "; String b = a.replace("a", "oa"); System.out.println(b); } } |
Result:
length() method
Used to determine the length of any string.
For example:
1 2 3 4 5 6 7 8 9 |
package com.huongdanjava.javaexample; public class Example { public static void main(String[] args) { String a = "Khanh"; System.out.println(a.length()); } } |
Result:
startsWith() method
This method used to check our string whether it begins with the character or string passed in this method?
The return value of this method is TRUE or FALSE.
For example:
1 2 3 4 5 6 7 8 9 |
package com.huongdanjava.javaexample; public class Example { public static void main(String[] args) { String a = "Khanh"; System.out.println(a.startsWith("K")); } } |
Result:
endsWith() method
Similar to the startsWith() method, but this method is intended to check whether our string must end with a character or a string passed in the method.
The return value of this method is also TRUE or FALSE.
For example:
1 2 3 4 5 6 7 8 9 |
package com.huongdanjava.javaexample; public class Example { public static void main(String[] args) { String a = "Khanh"; System.out.println(a.endsWith("n")); } } |
Result:
split() method
The split() method is used to split a certain string by using the separator character in the string. It will return us an array containing the values of the string after splitting.
For example, I have the following string “ab|cd|e”, to split this string with a separator “|”, I will code the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.javaexample; public class Example { public static void main(String[] args) { String s = "ab|cd|e"; String[] result = s.split("\\|"); for (String r : result) System.out.println(r); } } |
Result:
In case there are separators at the end of the string but the value between these separators is empty, to return these empty values fully, you need to use the overload method of this split() method with one more parameter has a negative value. Looking at the following example, you will clearly see the difference:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.huongdanjava.javaexample; public class Example { public static void main(String[] args) { String s = "ab|cd|e||f|||"; String[] result = s.split("\\|"); System.out.println(result.length); System.out.println("------"); String[] result1 = s.split("\\|", -1); System.out.println(result1.length); } } |
Result: