String.startsWith()
Used to detect whether a string starts with a specified prefix.
1 2 3 4 5 6 7 8 9 public class test { public static void main (String[] args) { String str = "Test string: hello world!" ; boolean flag1 = str.startsWith("Test" ); System.out.println("str starts with 'Test': " + flag1); boolean flag2 = str.startsWith("hello" ); System.out.println("str starts with 'hello': " + flag2); } }
The output of the code is as follows:
1 2 str starts with 'Test' : true str starts with 'hello' : false
String.endsWith()
Used to detect whether a string ends with a specified suffix.
1 2 3 4 5 6 7 8 9 public class test { public static void main (String[] args) { String str = "Test string: hello world!" ; boolean flag1 = str.endsWith("Test" ); System.out.println("str ends with 'Test': " + flag1); boolean flag2 = str.endsWith("!" ); System.out.println("str ends with '!': " + flag2); } }
The output of the code is as follows:
1 2 str ends with 'Test' : false str ends with '!' : true
String.contains()
Used to detect whether a specified character or string is contained in a string.
1 2 3 4 5 6 7 8 9 public class test { public static void main (String[] args) { String str = "Test string: hello world!" ; boolean flag1 = str.contains("test" ); System.out.println("str contains 'test': " + flag1); boolean flag2 = str.contains("string" ); System.out.println("str contains 'string': " + flag2); } }
The output of the code is as follows:
1 2 str contains 'test' : false str contains 'string' : true
String.split()
Split a string into a list.
1 2 3 4 5 6 7 8 9 public class test { public static void main (String[] args) { String a = "A,B,C,D,E,F,G" ; String[] b = a.split("," ); for (int i = 0 ; i < b.length; i++) { System.out.print(b[i] + " " ); } } }
The output of the code is as follows:
String.indexOf()
Returns the index of the first occurrence of the specified substring (or character) in a string. Returns -1 if there is no such substring (or character) in the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 public class test { public static void main (String[] args) { String str = "Test string: hello world!" ; System.out.println("str: " + str); System.out.println("str.length()=" + str.length()); Integer index1 = str.indexOf("t" ); System.out.println("indexOf('t'): " + index1); Integer index2 = str.indexOf("o" ); System.out.println("indexOf('o'): " + index2); } }
The output of the code is as follows:
1 2 3 4 str: Test string: hello world! str.length()=25 indexOf('t' ): 3 indexOf('o' ): 17
String.lastIndexOf()
Returns the index of the last occurrence of the specified substring (or character) in this string. Returns -1 if there is no such substring (or character) in the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 public class test { public static void main (String[] args) { String str = "Test string: hello world!" ; System.out.println("str: " + str); System.out.println("str.length()=" + str.length()); Integer index1 = str.lastIndexOf("t" ); System.out.println("lastIndexOf('t'): " + index1); Integer index2 = str.lastIndexOf("o" ); System.out.println("lastIndexOf('o'): " + index2); } }
The output of the code is as follows:
1 2 3 4 str: Test string: hello world! str.length()=25 lastIndexOf('t' ): 6 lastIndexOf('o' ): 20
StringUtils.substring()
Use String substring(String str, int start, int end)
to get the substring with index between [start, end).
1 2 3 4 5 6 7 8 9 10 public class test { public static void main (String[] args) { String str = "Test string: hello world!" ; System.out.println("str: " + str); System.out.println("str.length()=" + str.length()); String subStr = StringUtils.substring(str, 0 , 4 ); System.out.println("subStr: " + subStr); } }
The output of the code is as follows:
1 2 3 str: Test string: hello world! str.length()=25 subStr: Test