String str1= "Hello,world";
String str2= "Hello,world";
1. Comparison of strings:
Cases
System.out.println (Str1.compareto (str2));
System.out.println (Str1.comparetoignorecase (str2));
2. Searching for strings:
Note: the IndexOf () method is case-sensitive!
If the string value that you want to retrieve does not appear, the method returns-1.
When the same string appears more than once in a string, the value returned by this method is the index value of the position where the first occurrence occurs.
Cases
System.out.println (str1. IndexOf ("w"));
System.out.println (Str1.lastindexof ("O"));
3. Find a character in a location:
Cases
System.out.println (Str1.charat (1)); Output: H
4. Get the length of the string:
Cases
System.out.println (Str1.length ());
5. Intercept the string:
str.substring (int startIndex)
str.substring (int startindex,int endIndex)
Cases
System.out.println (str1.substring (1));
System.out.println (str1.substring (1,3));
6. String Case Conversion:
Convert str.tolowercase () to lowercase
Convert str1.touppercase () to uppercase
7. Substitution of strings:
Note: replace (charsequence target, charsequence replacement), replaces all target with replacement, and two parameters are strings.
ReplaceAll (string regex, string replacement), replacing all regex matches with replacement, the Regex is obviously a regular expression, and replacement is a string.
Replacefirst (string regex, string replacement), basically the same as ReplaceAll, except that only the first match is replaced.
Cases
System.out.println (Str2.replace ("ll", "Y")); Output: Heyo,world
8. Segmentation of Strings:
Split (String regex)
Note: If you use "." As a separate word, it must be written as follows, String.Split ("\ \"), so that the correct separation can not be used String.Split (".");
If you use the ' | ' As a separate word, must be the following wording, String.Split ("\\|"), so that the correct separation, can not be used String.Split ("|"); “.” and "|" is an escape character and must be added "\ \";
If you have more than one delimiter in a string, you can use the ' | ' As a hyphen, for example, "Acount=?" and UU =? Or n=? ", the three are separated out, you can use String.Split (" And|or ");
Cases
System.out.println (Arrays.tostring (Str1.split (",")));
9, Equals () and = =
The Equals () method compares the characters in a string object, and the = = operator Compares whether two objects refer to the same instance.
example, String s1= "Hello";
String S2=new string (S1);
S1.eauals (S2); True
S1==S2; False
10. Connection of Strings:
Cases
System.out.println (Str1.concat (str2));
Stringbuffer/stringbuilder type *******************
11. Deletion of Strings:
Note: string-type strings cannot be modified and must be StringBuffer or StringBuilder type.
StringBuffer Delete (int startindex,int endIndex)
StringBuffer deletecharat (int loc)
Cases
System.out.println (Str11.delete (0,3));
12. Insert a character or string into an existing string:
StringBuffer Insert (int index,string str)
StringBuffer insert (int index,char ch)
StringBuffer Insert (int index,object obj)
Cases
System.out.println (Str11.insert (0, "yyy"));
13. Substitution of strings:
Note: A string substitution of type StringBuffer can replace a string.
StringBuffer replace (int startindex,int endindex,string str)
Cases
System.out.println (Str11.replace (2,5, "ha")); Heha,world
14. Adding a string:
StringBuffer append (String str)
Cases
System.out.println (Str11.append ("pp"));
15. Reversal of the string:
StringBuffer reverse ()
Cases
System.out.println (Str11.reverse ());
Common string Operations in Java