Common Operations of the String class and common operations of the String class
I. Overview of the String class
1. The value of String is an object and a constant cannot be changed.
2. The equals method of String is to compare the content of two strings.
3. String s1 = "abc" String s2 = new String ("abc") s1 has an object and s2 has two objects.
Ii. Common functions of String-acquisition and judgment
1. Get
The number of characters contained in a 1.1 string, that is, the length of the string.
Int length (): Get the length.
1.2 obtain a character at a specified position
Char charAt (int index)
1.3 obtain the position of the character in the string based on the Character
Int indexOf (int ch): returns the location where ch first appears in the string.
Int indexOf (int ch, int fromIndex): obtains the position where ch appears in the string starting from the position specified by fromIndex.
Int indexOf (String str, int fromIndex): obtains the position where ch appears in the String starting from the position specified by fromIndex. If no position is found,-1 is returned.
Int lastIndexOf (String str): reverse index where a character appears
1 public static void method_get () {2 String str = "abcdeakpf"; 3 // print the String length 4 sop (str. length (); // The result is 5 5 // obtain characters 6 sop (str. charAt (4); // The result is a 7 // obtain the index 8 sop (str. indexOf ('A', 3); // The result is 5 9 // reverse index the position where a character appears 10 sop (str. lastIndexOf ("a"); // result: 511}View Code
2. Judgment
2.1 whether the string contains a substring
Boolean contains (str)
Special: indexOf (str): You can index the position where str appears for the first time. If-1 is returned, this str does not exist in the string, therefore, it can also be used to determine whether a specified character substring is contained and obtain the position where the substring appears;
Whether there is content in 2.2 characters
Boolean isEmpty (): determines whether the length is 0.
2.3 whether the string starts with the specified content
Boolean startsWith (str );
2.4 The string ends with the specified content
Boolean endsWith (str );
2.5 check whether the string content is the same. The equals method in the Object class is rewritten.
Boolean equals (str );
2.6 check whether the content is the same. Ignore case sensitivity.
Boolean inclusignorecase ();
1 public static void method_is () {2 String str = "ArrayDemo. java "; 3 // determine whether the file name starts with Array 4 sop (str. startsWith ("Array"); // returns true5 // determines whether the file name is. java file 6 sop (str. endsWith (". java "); // The result is true7 // determine whether the file name contains the Demo Word 8 sop (str. contains ("Demo"); // The result is true9}View Code
3. String common functions-Conversion
3.1 convert the character array into a string.
Constructor: String (char [])
String (char [], offset, count): convert part of the character array into a String
Static Method:
Static String copyValueOf (char [])
Static String copyValueOf (char [], offset, count)
Static String valueOf (cahr [])
3.2 convert a string into a character array
Char [] toCharArray ();
3.3 convert byte arrays into strings
String (byte [])
String (byte [], offset, count): convert part of the byte array into a String
3.4 convert a string to a byte array
Byte [] getBytes ();
3.5 convert the basic data type to a string
Static String valueOf (int)
Static String valueOf (double)
3.6 Special: encoding tables can be specified during conversion of strings and byte arrays.
1 public static void method_trans () {2 char [] arr = {'A', 'B', 'C', 'D', 'E', 'F '}; 3 String s = new String (arr, 1, 3); 4 sop ("s =" + s); // The result is bcd 5 6 String s1 = "zxcvbnm "; 7 char [] chs = s1.toCharArray (); 8 for (int I = 0; I <chs. length; I ++) {9 sop ("ch =" + chs [I]); 10} 11}View Code
4. Replace
String replace (oldchar, newchar)
If the character to be replaced does not exist, the original string is returned.
1 public static void method_replace () {2 String s = "Hello java"; 3 String s1 = s. replace ('A', 'n'); 4 String s2 = s. replace ("java", "world"); 5 sop ("s =" + s); // The result is Hello java6 sop ("s1 =" + s1 ); // The result is Hello jnvn7 sop ("s2 =" + s2); // The result is Hello world8}View Code
5. Cutting
String [] split (regex );
1 public static void method_split () {2 String s = "zhangsan, lisi, wangwu"; 3 String [] attr = s. split (","); 4 for (int I = 0; I <attr. length; I ++) {5 sop (attr [I]); // The result is zhangsan lisi wangwu6} 7}View Code
6. substring to obtain part of the string
String substring (begin): starts from the specified position and ends. If the badge does not exist, an exception occurs when the badge is out of bounds.
String substring (begin, end): Starting from the start position to the end position, protecting the start position from not including the end position
1 public static void method_sub () {2 String s = "abcdef"; 3 sop (s. substring (2); // The result is cdef4 sop (s. substring (2, 4); // The result is cd5}View Code
7. Convert, remove spaces, and compare
7.1 convert the string to uppercase or lowercase letters
String toUpperCase ();
String toLowerCase ();
7.2 remove multiple spaces at both ends of the string
String trim ();
7.3 compare the natural order of two strings
Int compareTo (String)
1 public static void method_7 () {2 String s = "Hello Java"; 3 sop (s. toUpperCase (); 4 sop (s. toLowerCase (); 5 sop (s. trim (); 6 String s1 = "acc"; 7 String s2 = "aaa"; 8 sop (s1.compareTo (s2 )); // The result is 2 (the result of two Unicode values subtraction), indicating that s1 is greater than s29}View Code