Compare, find, replace, split and intercept Java strings
1, String comparison
1. equals(): case sensitive, and the return value type is boolean;
//Case sensitive comparison String str1 = "hello" ; String str2 = "Hello" ; System.out.println(str1.equals(str2)); // Return false
2. Case insensitive: equalsIgnoreCase(), the return value type is boolean;
//Case insensitive comparison String str1 = "hello" ; String str2 = "Hello" ; System.out.println(str1.equalsIgnoreCase(str2)); // Return true
3. Compare the size relationship between two strings: compareTo(), the return value type is int;
This method will return three contents according to the size relationship:
Equal: return 0;
Less than: the return value is less than 0;
Greater than: the return value is greater than 0;
//compareTo comparison; System.out.println("A".compareTo("a")); // -32 System.out.println("a".compareTo("A")); // 32 System.out.println("A".compareTo("A")); // 0 System.out.println("AB".compareTo("AC")); // -1 System.out.println("Liu".compareTo("Yang"));
2, String lookup
1. Contains (charsequences): judge whether a substring exists, and the return value type is boolean;
String str = "helloworld" ; System.out.println(str.contains("world")); // true
2. indexOf(String str): finds the position of the specified string from the beginning. If the start index of the return position is found, it returns - 1. The return value type is int;
// Use the indexOf() method to find the location String str = "helloworld" ; System.out.println(str.indexOf("world")); // Returns the index from 5,w System.out.println(str.indexOf("bit")); // Return - 1, not found if (str.indexOf("hello") != -1) { System.out.println("You can find the specified string!"); } //Now it is basically done with the contains() method. //When using indexOf(), it should be noted that if the content is repeated, it can only return to the first location of the search.
// Considerations for using indexOf() String str = "helloworld" ; System.out.println(str.indexOf("l")); // 2 //When searching, we often judge the beginning or end.
3. indexOf(String str,int fromIndex): find the string position from the specified position, and the return value type is int;
String str = "helloworld" ; System.out.println(str.indexOf("l",5)); // 8
4. lastIndexOf(String str): find the position of the substring from the back to the front, and the return value type is int;
String str = "helloworld" ; System.out.println(str.lastIndexOf("l")); // 8
5. lastIndexOf(String str,int fromIndex): find the position of the substring from back to front from the specified position, and the return value type is int;
String str = "helloworld" ; System.out.println(str.lastIndexOf("l",6)); // 3
6. startsWith(String prefix): judge whether to start with the specified string, and the return value type is boolean;
String str = "**@@helloworld!!" ; System.out.println(str.startsWith("**")); // true
7. startsWith(String prefix,int toffset): judge whether it starts with the specified string from the specified position, and the return value type is boolean;
String str = "**@@helloworld!!" ; System.out.println(str.startsWith("@@",2)); // ture
8. endsWith(String suffix): judge whether to end with the specified string, and the return value type is boolean;
String str = "**@@helloworld!!" ; System.out.println(str.endsWith("!!")); // true
3, String replacement
Replace the existing string data with a specified new string.
1. replaceAll(String regex, String replacement): replace all specified contents and return the replaced string;
String str = "helloworld" ; System.out.println(str.replaceAll("l", "_")); //Output - > he__ owor_ d
2. replaceFirst(String regex, String replacement): replace the first content and return the replaced string;
String str = "helloworld" ; System.out.println(str.replaceFirst("l", "_"));//Output - > he_ loworld
Note: since the string is an immutable object, the replacement does not modify the current string, but generates a new string;
4, Splitting of strings;
A complete string can be divided into several substrings according to the specified separator.
1. split(String regex): splits all strings, and the return value type is String [];
//Split the string String str = "hello world hello bit" ; String[] result = str.split(" ") ; // Split by space for(String s: result) { System.out.println(s); }
output
2. split(String regex, int limit): split the String part. The length of the split array is the limit of the limit, and the return value type is String [];
//Partial splitting of strings String str = "hello world hello bit" ; String[] result = str.split(" ",2) ; for(String s: result) { System.out.println(s); }
output
Splitting is a very common operation. You must focus on it. In addition, some special characters may not be segmented correctly as separators, and escape needs to be added
//Split IP address String str = "192.168.1.1" ; String[] result = str.split("\\.") ; for(String s: result) { System.out.println(s); }
output
matters needing attention
1. The characters "|", "*", "+" must be preceded by the escape character ";
2. If it is "", it must be written as "";
3. If there are multiple separators in a string, you can use "|" as a hyphen;
// Multiple split String str = "name=zhangsan&age=18" ; String[] result = str.split("&") ; for (int i = 0; i < result.length; i++) { String[] temp = result[i].split("=") ; System.out.println(temp[0]+" = "+temp[1]); }
output
5, String interception
Extract part of the content from a complete string.
1. substring(int beginIndex): intercept from the specified index to the end;
String str = "helloworld" ; System.out.println(str.substring(5));
2. substring(int beginIndex, int endIndex): intercepts some contents from the specified index;
String str = "helloworld" ; System.out.println(str.substring(0, 5));
matters needing attention:
1. Index starts at 0
2. Pay attention to the writing method of the front closed and back open interval. substring(0, 5) indicates a character containing subscript 0, but not subscript 5