Chapter 2 string and Chapter 2 string
1. Methods for declaring strings:
String s = "hello ";
String s = new String ("hello ");
2. String Length
String. length ();
3. String comparison
String. equals (string 2 );
Returns a boolean value if the values of the two strings are equal. If the values are the same, true is returned; otherwise, false is returned.
4. equals and =
Equals comparison value to determine whether the values of two string objects are equal. It is generally used for string comparison;
= Compare the address, that is, to determine whether it is the same string, the object is generally used for the comparison of numbers.
Note:
1) compare with equals (), and set the value to the front to prevent null string errors (null strings cannot be called by equals ).
2) The result returned by calling the length () method is the number of characters in the string.
3) perform equas comparison and pay attention to the string size. If the case is different, false is returned. Then, the equalsIgnoeCase () method can be used to ignore the case sensitivity of characters.
5. Ignore case sensitivity when comparing repeated signore
ToLowerCase () to lowercase, for example: name. tolowerCase ();
ToUpperCase () is converted to uppercase, for example, name. toUpperCase ();
6. Complex string processing
\: Escape Character
Concatenate a string using the "+" or concat () method. When the original string is a valid string and then concatenates a new string, a copy is created:
Example: String str = "hello"; str = str + "Beijing ";
String str = new Sting ("hello ");
Str. concat ("Beijing ");
Using the "+" and concat () methods will cause a waste of memory, poor performance, but thread security.
Note:
When the "+" operator is used to connect String and int (or double) data, "+" automatically converts int (or double) data to String type.
7. String extraction and query
Index (parameter): returns the subscript of a character or string.
IndexOf () method
This method is used to search for a character or string in a string. It returns the position where the first matching word appears. If no match is found,-1 is returned. The call is yes, name of the character (or string) to be searched in parentheses.
Example:
String s = "No regrets ";
Int index = s. indexOf ("Spring ");
After execution, the return character "Spring" is 1.
8. lastIndexOf () method:
Returns the subscript of the last character or string.
For example: String s = "Youth Without regret or regret ";
Int index = s. lastIndexOf (" ");
After the command is executed, the first character position of the string "Youth" is returned, and the index value is 6;
9. subString (parameter): truncates a string.
Substring (int index); subscripts start from 0 and end from index.
For example, you need to extract "spring without regret" from the string "Youth Without regret ".
String s = "No regrets ";
String result = s. substring ();
10. substring (int beginIndex, int endIndex); subscript starts from 0
Starting from beginIndex, including the beginIndex position
End with endIndex, excluding endIndex
For example, you need to extract "no regrets, no regrets" from "no regrets, no regrets ";
String s = "Youth Without regret or regret ";
String result = s. substring (2, 6 );
11. trim () method
Remove spaces before and after the string
12. String splitting
Split (): splits the string and returns an array.
For example, String str = "long ting Wai-ancient road edge-grass green sky-stay bottom ";
String [] word = str. split ("-");
For (int I = 0; I <word. length; I ++ ){
System. out. println (word [I]);
}
13. Use the StringBuffer class for frequent concatenation of Characters
Example: StringBuffer sbuf = new StringBuffer ("you ");
Sbuf. apped ("OK ?");
Sbuf. apped (1, "");
String str = sbuf. toString ();
System. out. println (str );
Features: append strings to itself without generating copies, saving memory, high performance, and county safety