6) Java's String class and javastring class
1) String object initializationBecause String objects are very commonly used, Java provides a special simplified syntax when initializing String objects. The format is as follows:
String s = "abc ";
S = "Java language ";
In fact, according to the standard object-oriented syntax, the format should be:
String s = new String ("abc ");
S = new String ("Java language ");
There is a large waste of memory usage according to the object-oriented standard syntax. For example, String s = new String ("abc"); in fact, two String objects are created. One is an "abc" object, which is stored in the constant space, one is the space applied for by the object s using the new keyword.
2) Common Operations on strings
A) charAt Method
The function of this method is to obtain the specified characters in the string according to the index value (the index value of the first character in the string is 0, the index value of the second character is 1, and so on. For example:
String s = "abc ";
Char c = s. chatAt (1 );
The value of variable c is 'B '. B) compareTo Method
This method is used to compare the size of two strings. The comparison principle is to compare the character encoding of each character in sequence. First, compare the first character of two strings. If the character encoding of the first string is greater than that of the second string, a value greater than 0 is returned, if the value is smaller than 0, a value smaller than 0 is returned. If the value is equal, a subsequent character is compared. If the two strings have the same character encoding, 0 is returned.
String s = "abc"; String s1 = "abd"; int value = s. compareTo (s1); then the value is less than 0, that is,-1.
A similar method compareToIgnoreCase exists in the String class. This method ignores the case sensitivity of characters for comparison. The comparison rules are the same as those of compareTo. For example:
String s = "aBc"; String s1 = "ABC"; int value = s. compareToIgnoreCase (s1); then the value is 0, that is, the two strings are equal.
C) concat Method
This method connects two strings to form a new string. For example:
String s = "abc"; String s1 = "def"; String s2 = s. concat (s1); the value of the new string s2 generated after the connection is "abcdef", and the values of the string s and s1 are not changed. To connect multiple strings, use the following method:
String s = "abc"; String s1 = "def"; String s2 = "1234"; String s3 = s. concat (s1 ). concat (s2); the value of the new string s3 is "abcdef1234 ".
In fact, in actual use, the Syntax provides a simpler form, that is, using "+" to connect strings. For example:
String s = "abc" + "1234 ";
The value of string s is "abc1234", which makes writing easier and more intuitive.
In addition, you can use "+" to connect not only strings, but also other types. However, when the connection is required, at most one content involved in the connection is string type. In addition, the order of "+" matching is from left to right. If the content connected to both sides is of the basic numeric type, the addition operation is performed, if one of the content involved in the connection is a string, it is connected according to the string.
For example:
Int a = 10;
String s = "123" + a + 5;
The value of string s after connection is "123105". The calculation process is to first connect the value of string "123" and variable a, and generate the string "12310 ", connect the string to number 5 to generate the final result.
The following code:
Int a = 10;
String s = a + 5 + 123 ";
After the connection, the value of string s is "15123". The calculation process is to calculate a and number 5 first. because both are numeric, the addition operation or number value 15 is performed, then use the number value 15 and the string "123" to connect to obtain the final result.
The following connection code is incorrect:
Int a = 12;
String s = a + 5 +'s ';
Because there is no string involved in the connection, the calculated result is a numeric value, and a numeric value cannot be assigned to string s during the value assignment.
D) endsWith Method
This method is used to determine whether a string ends with a certain string. If it ends with a corresponding string, true is returned.
For example:
String s = maid ";
Boolean B = s. endsWith ("doc ");
The value of variable B is true.
E) equals Method
This method is used to determine whether the content of the two string objects is the same. If the values are the same, true is returned. Otherwise, false is returned. For example:
String s = "abc ";
String s1 = new String ("abc ");
Boolean B = s. equals (s1 );
"=" Is used to compare whether the addresses of the two objects in the memory are the same. For example, in the above Code, if the judgment is:
Boolean B = (s = s1 );
The value of variable B is false, because the address of the s object is the "abc" address, and s1 uses the new Keyword to apply for a new memory, therefore, the memory address is different from the "abc" Address of s, so the obtained value is false.
A similar method exists in the String class.
EqualsIgnoreCaseThis method is used to ignore the case sensitivity and compare whether the content of the two strings is the same. For example:
String s = "abc ";
String s1 = "ABC ";
Boolean B = s. equalsIgnoreCase (s1 );
The value of variable B is true.
F) getBytes Method
The function of this method is to convert a string to a corresponding byte array to facilitate data storage and transmission. For example:
String s = "computer ";
Byte [] B = s. getBytes (); // use the local default string to convert it to a byte array
Byte [] B = s. getBytes ("gb2312"); // use the gb2312 character set to convert to a byte array
In actual conversion, you must pay attention to the character set problem. Otherwise, Chinese characters may encounter problems during conversion.
G) indexOf Method
This method is used to find the starting position (array subscript) of a specific character or string in the current string. If it does not exist,-1 is returned. For example:
String s = "abcded ";
Int index = s. indexOf ('D ');
Int index1 = s. indexOf ('H ');
Returns the position where character d appears for the first time in string s. The value is 3. Because the character h does not exist in string s, the value of index1 is-1.
Of course, you can also find the corresponding characters from a specific location, for example:
Int index = s. indexOf ('D', 4 );
Search for the first character d that appears after the index value 4 (including 4) in string s, then the index value is 5.
Because indexOf is overloaded, you can also find the starting position of a specific string in the current string, in the same way as searching for characters.
Another similar method is the lastIndexOf method, which is used to search for the specified character or string that appears for the first time starting from the end of the string. For example:
String s = "abcded ";
Int index = s. lastIndexOf ('D ');
The index value is 5.
H) length Method
This method returns the length of a string, that is, the number of characters in the string.
The Chinese character is also a character. For example:
String s = "abc ";
String s1 = "Java language ";
Int len = s. length ();
Int len1 = s1.length ();
The value of len is 3, and the value of len1 is 6.
I) replace Method
This method is used to replace all specified characters in the string and generate a new string. After this method is called, the original string does not change. For example:
String s = "abcat ";
String s1 = s. replace ('A', '1 ');
The purpose of this Code is to replace all character a in string s with character 1, and the value of the new string s1 is "1bc1t", while the content of string s remains unchanged.
If you want to replace a specified string with another string, you can use the replaceAll method, for example:
String s = "abatbac ";
String s1 = s. replaceAll ("ba", "12 ");
This code replaces all strings "ba" in string s with "12" to generate a new string "a12t12c", while
The content of string s does not change either..
If you only need to replace the first specified string, you can use the replaceFirst method, for example:
String s = "abatbac ";
String s1 = s. replaceFirst ("ba", "12 ");
The purpose of this Code is to replace "AB", the first occurrence string in string s, with "12", then the value of string s1 is "a12tbac ", the content of string s does not change.
J) split method
This method uses a specific string as the interval to split the content of the current string. Generally, a string array is obtained after the split. For example:
String s = "AB, 12, df ";
String s1 [] = s. split (",");
The function of this Code is to split string s using string "," as the interval to obtain the string number s1 after the split. The content of this Code is {"AB", "12 ", "df "}.
This method is the basic method for parsing strings.
If the content in the string contains the same content as the interval string, the empty string at the end is ignored. For example:
String s = "abbcbtbb ";
String s1 [] = s. split ("B ");
The split result string s1 contains {"a", "," c "," t "}. The number of empty strings in the middle of the split is equal to the number of strings in the middle interval minus one. For example:
String s = "abbbcbtbbb ";
String s1 [] = s. split ("B ");
The split result is {"a", "c", "t "}. The last empty string is ignored no matter how many strings exist.
To limit the number of strings after splitting, you can use another split method, for example:
String s = "abcbtb1 ";
String s1 [] = s. split ("B", 2 );
The function of this Code is to split string s into a maximum of two string arrays. The result is {"a", "cbtb1 "}.
If the second parameter is negative, as many strings as possible will be split, including the trailing empty strings.
K) startsWith MethodThis method is similar to the endsWith method, but it is used to determine whether a string starts with a certain string. For example:
String s = "TestGame ";
Boolean B = s. startsWith ("Test ");
The value of variable B is true.
I) substring Method
This method is used to take the "substring" in the string. The so-called "substring" is a part of the string. For example, "23" is the substring of the string "123.
There are 6 substrings of string "123": "1", "2", "3", "12", "23", and "123 ". "32" is not a substring of the string "123.
For example:
String s = "Test ";
String s1 = s. substring (2 );
The purpose of this Code is to take all the characters after the index value of string s is 2 (inclusive) as the substring, then the value of string s1 is "st ".
If the value of a number is the same as the length of a string, an empty string is returned. For example:
String s = "Test ";
String s1 = s. substring (4 );
The value of string s1 is "".
To take a portion of the string, you can use the substring method with two parameters, for example:
String s = "TestString ";
String s1 = s. substring (2,5 );
The function of this Code is to take the part of the string s starting from index value 2 (included) to index value 5 (not included) as the sub-string, the value of string s1 is "stS ".
The following is a simple application code that outputs all substrings of any string. The Code is as follows:
String s = "substring example ";
Int len = s. length (); // obtain the string length.
For (int begin = 0; begin <len-1; begin ++) {// start index value
For (int end = begin + 1; end <= len; end ++) {// end index value
System. out. println (s. substring (begin, end ));
}
}
In this Code, the Circular Variable begin indicates the starting index value of the substring to be obtained, the change interval ranges from the index value 0 of the first character to the index value len-2 of the last two strings, and end indicates the end index value of the desired substring, the change interval ranges from the subsequent index value to the string length. Loop nesting allows you to traverse all substrings in a string.
M) toCharArray Method
This method is similar to the getBytes method, which converts a string to a corresponding char array. For example:
String s = "abc ";
Char [] c = s. toCharArray ();
The value of character array c is {'A', 'B', 'C '}.
N) toLowerCase Method
This method converts all uppercase characters in a string to lowercase letters. For example:
String s = "AbC123 ";
String s1 = s. toLowerCase ();
The value of string s1 is "abc123", while the value of string s remains unchanged.
A similar method is toUpperCase. The function of this method is to convert lowercase characters in a string to corresponding uppercase characters. For example:
String s = "AbC123 ";
String s1 = s. toUpperCase ();
The value of string s1 is "ABC123", and the value of string s remains unchanged.
O) trim method
This method removes all spaces at the beginning and end of the string and forms a new string. This method does not remove spaces in the string. For example:
String s = "abc 123";
String s1 = s. trim ();
The value of string s1 is "abc 123 ". The value of string s remains unchanged.
P) valueOf Method
This method is used to convert data of other types to the string type. It should be noted that the syntax of forced type conversion before cannot be used between basic data and string objects.
In addition, because this method is a static method, you do not need to create a String type object. For example:
Int n = 10;
String s = String. valueOf (n );
The value of string s is "10 ". Although there is no change for programmers, the Data Type of the program has changed.
This article introduces a simple application. The logic code for determining the number of digits in a natural number is as follows:
Int n = 12345;
String s = String. valueOf (n );
Int len = s. length ();
The string length len represents the number of digits of the natural number. This kind of judgment is simpler than the mathematical Judgment Method in logic.
For more information about the use of the String class, see the corresponding API documentation for details about other methods and the methods described here.