String object
Declaration and initialization
1 String str = "abc"; 2 String str = new String("abc"); 3 char[] a = {'a', 'b', 'c'}; 4 String str = new String(a); 5 String str = new String(a,0,1);
String concatenation: The "+" sign, other data types will automatically call toString method, converted into a string connected
Get string length:
str.length();
String search
1 String S = "the Add" ; 2 s.indexOf ( "D"); // the specified index of the first occurrence of the character string, returns a parameter 0 returns an empty string . 3 s.lastIndexOf ( "D") // Specify index of the last occurrence of the string, returns 2, the string length parameter returns an empty string
Get the character specified index
s.charAt (. 1); // returns d
Get a substring
. 1 s.substring (. 1); // returns dd, take the default character string to the end of 2 s.substring (0,. 3); // returns the Add . 3 s.substring (0, 2); // return ad, attention is take the end of an index
White spaces
s.trim (); // remove the string end to end across a space, ps: in fact, it not only spaces can be removed, in particular Now
String replacement
. 1 s.replace does ( "A", "A"); // returns the Add 2 s.replace does ( "AD", "the AD"); // Returns ADD . 3 s.replaceAll ( "D", "HH"); // return ahhhh, i.e. replace all substrings . 4 s.replaceFirst ( "D", "HH"); // return ahhd, i.e., replacing the first substring
Determining whether the start and end of the string is specified string
. 1 s.startswith ( "AD"); // Returns to true 2 s.startswith ( "D"); // returns to false . 3 s.startswith ( "AD", 0); // returns true, determining whether or not the specified index start with the specified string . 4 s.endsWith ( "D"); // returns true
Determining whether the strings are equal --- == determines whether the same address string
. 1 s.equals ( "the Add"); // to true, if two empty string comparison, returns to true 2 s.equals ( "the Add"); // case sensitive . 3 s.equalsIgnoreCase ( "ADD"); // to true, ignore case
Lexicographically comparing strings --- based on Unicode value of the character
1 s.compareTo("dad");//-3 2 s.compareTo("add");//0 3 s.compareTo("Add");//32 4 s.compareTo("ad");//1
--- Digital and non-case conversion characters do not affect
. 1 s.toUpperCase (); // the ADD 2 s.toLowerCase (); // converted to lowercase
String split
1 String s = "adadads"; 2 String[] a = s.split("d");//a a a s 3 String[] a = s.split("d",2);//a adads
Format string --- format (format, args);
Example:
= String.format formatted String ( " % S% d years old this year. " , " Mike " , 30 ); // . "This year 30-year-old Li"
- This method is the first parameter string format, the latter parameters are parameter string format, the format string for replacing the placeholder.
- Placeholder expressed in the form "% x" of, different types of parameters to use different letters.
String.format()
The return value is a string type, the result is formatted.
The date and time format --- Reference
https://segmentfault.com/a/1190000013654676
String builder --- solve the "+" to generate a new String object, increase system overhead issues, the generator can generate a sequence of characters of variable length, additional strings are frequently recommended
. 1 the StringBuilder STR = new new the StringBuilder ( " A " ); 2 str.append ( " C " ); // string additional content, ac-- return parameter can be any type . 3 str.insert ( 0 , to true ); // add characters specified position, returns trueac . 4 str.delete ( 0 , 2 ); // delete the character string designated area, return ueac . 5 str.deleteCharAt ( 2 ); // delete the specified location string. Return treac
StringBuilder and StringBuffer main difference: on the security thread, StringBuilder is not thread-safe, while StringBuffer is thread-safe
If the operation to be performed is multi-threaded, then we should use StringBuffer, but in single-threaded case, it is recommended to use the faster StringBuilder.
String: apply a small amount of operation of the string
StringBuilder: apply to the situation of a large number of operating under a single thread in the character buffer
A large number of cases suitable for multi-threaded operating under the character buffer: StringBuffer