String, string
It mainly involves the StringBuffer class that cannot be changed after the program runs initialization and the StringTokenizer class that can be dynamically changed by the String class String and the String content, and the String Conversion lexical analysis class StringTokenizer; it also describes the conversion between strings and other data types.
In Java, strings are processed as objects, and a series of methods are encapsulated in the objects for string processing.
String type
String is located in the java. lang package, so you can use String to instantiate the object without using the import Statement in the program. The String class is mainly used to process strings that cannot be changed after initialization.
String Construction
String constants are a series of valid java characters separated by double quotation marks. When the value assignment operator is used to initialize a String, JVM automatically generates a String class instance for each String. The string declaration format is as follows:
String s;
You can use the constructor of the String class to create a String.
S = new String ("xjtu ");
You can also write it as follows:
S = "xjtu ";
Declarations and instantiation objects can be completed in one step.
Using the constructor provided by the String class, you can generate null strings or other basic data types to generate String objects.
(1) Use the following method to generate an empty String instance.
String str = new String ();
(2) In the constructor provided by the String class, strings can be constructed by character arrays, byte arrays, and String buffers, as shown below:
Char c1 [] = {'1', '2', '3', '4 '};
Char c2 [] = {'1', '2', '3', '4', '5 '};
String str1 = new String (c1 );
String str2 = new String (c2, 0, 3 );
System. out. print (str1 );
System. out. print (str1 );
Result: "1234"
(3) generate a string using Byte Arrays:
Byte c1 [] = {66,67, 68 };
Byte c2 [] = {65,66, 67,68 };
String str1 = new String (c1 );
String str2 = new String (c2, 1, 3 );
System. out. print (str1 );
System. out. print (str1 );
The result is BCD.
Common Methods of the String class
The String class provides methods such as length (), charAt (), indexOf (), lastIndexOf (), getChars (), getBytes (), and toCharArray. These methods include string length calculation, string comparison, string retrieval, string truncation, replacement, and other methods based on their purposes.
String Length Calculation
You can use the length () method in the String class to obtain the length of a String. Definition:
Public int length ()
This method returns the string's16-bit Unicode characters. For example:
String s = "xjtu ";
S. length () is 4
String comparison
String comparison methods include equals (), inclusignorecase (), startsWith (), endsWith (), regionMatches (), compareTo (), and compareToIgnoreCase.
1) equals () and equalsIgnoreCase () Methods
Equals () Definition:
Public boolean equals (String s)
This method is used to compare whether the object of the current string is the same as the object of the string s specified by the parameter. Note the difference between the equals () method and "=" in the String class.
EqualsIgnoreCase () Definition:
Public boolean inclusignorecase (String s)
The String object calls equalsIgnoreCase (String s) to compare whether the current object is the same as the String s specified by the parameter. Case sensitivity is ignored during comparison.
2) startsWith () and endsWith () Methods
The string object calls the startsWith () and endsWith () methods to determine whether the prefix and suffix of the current String object are strings specified by the parameter s.
Definition:
Public boolean startsWith (String s) and public boolean endsWith (String s );
For example:
String str1 = "20150418 ";
String str2 = "20140418 ";
The value of str1.startsWith ("2015") is true;
The value of str2.endsWith ("0418") is true;
3) regionMatches () method
The Declaration format is as follows:
Public boolean regionMatches (int firstStart, String other, int otherStart, int length) and public boolean regionMatches (boolean B, int firstStart, String other, int otherStart, int length)
Starting from the position specified by the current specified string parameter firstStart, take a substring of length and compare it with a substring specified by the parameter other. The substring specified by other is a substring starting from the position specified by the otherStart parameter and obtaining the length from other as length. True is returned if the two substrings are the same; otherwise, false is returned.
4) compareTo () and compareToIgnoreCase () Methods
Statement format:
Public int compareTo (String s)
Public int compareToIgnoreCase (String s)
The compareTo () method compares the size of a string specified by parameter s in alphabetical order. If the current string is the same as s, 0 is returned. If it is greater than s, a positive value is returned. If it is less than s, a negative value is returned. Note that lowercase letters are greater than uppercase letters, while compareToIgnoreCase () is case-insensitive.
String Retrieval
Searches the position where a specified character or string appears in the string. It is used to locate a character or string in the string. The method declaration format is as follows:
Public int indexOf (int ch)
Public int indexOf (int ch, int fromIndex)
Public int indexOf (String str)
Public int indexOf (String str, int fromIndex)
The preceding four methods are used to locate the specified character and string in the string, and you can use fromIndex to specify the starting address of the match in the method. If no character or string is retrieved, the return value of this method is-1.
String s = "xjtuxjtu ";
Int I;
I = s. indexOf ('x'); // 0
I = s. indexOf ("xjtu", 0); // 0
I = s. indexOf ('xjtu', 4); // 4
In addition, the String class also provides the final position of the String. The method declaration format is as follows:
Public int lastIndexOf (int ch)
Public int lastIndexOf (int ch, int fromIndex)
Public int lastIndexOf (String str)
Public int lastIndexOf (String str, int fromIndex)
The above four methods are used to locate the specified character and the position of the last occurrence of the string in the string. In the above method, fromIndex can be used to specify the starting position of the matching. If no character or string is retrieved, the return value of this method is-1.
String Truncation
Truncates a substring in a string. The declaration format is as follows:
Public String substring (int beginIndex)
This method obtains a substring of the current string, which is obtained from the beginIndex of the current string.
Public String substring (int beginIndex, int endIndex)
This method obtains a substring of the current string, which is truncated from the beginIndex of the current stringEndIndex-1The string obtained from the terminal.
String replacement
Public String replace (char oldChar, char newChar)
String object s calls this method to obtain a String object. This string object is a string obtained by replacing oldChar in s with the character specified by newChar.
Public String replace (String old, String new)
The string object s calls this method to obtain a String object. This string object replaces the string specified by the old parameter in s with the string specified by the new parameter.
String s = "xjtuxjtu ";
System. out. println (s. replace ('x', 's'); // sjtusjtu
System. out. println (s. replace ("xjtu", "Tsinghua"); // TsinghuaTsinghua
Public String trim ()
A string s gets a String object by calling the trim () method. This string object is a string after s removes spaces.
Other methods
1) string case-sensitive Conversion
The Declaration format is as follows:
Public String toUpperCase (Local local) // only converts a specified position
Public String toUpperCase ()
Public String toLowerCase (Local local) // only converts a specified position
Public String toLowerCase ()
2) convert to a String Array
The Declaration format is as follows:
Public char [] toCharArray ()
This method is used to convert a string to a string array. The Return Value Type of this method is a character array, as follows:
String str = new String ("I love xjtu ");
Char [] ch;
Ch = str. toCharArray ();
System. out. print (ch); // I love xjtu
3) Conversion between string and character array
Statement format:
GetChars (int srcBegin, int srcEnd, char [] dst, int dstBegin)
This method is used to copy the character content in the string to the character array. SrcBegin is the starting position of the copy, the srcEnd-1 is the ending position of the copy, the string value dst is the destination character array, and dstBegin is the starting position of the copy of the destination character array. The original data is overwritten during replication.
4) connect two strings
The Declaration format is as follows:
Public String concat (String str)
This method is used to connect two strings, which have the same function as the "+" operator of the string.