Section 1. String object 1. Use a string as a string object in Java. When creating a String object, the created string cannot be changed. Each time you need to change the string, you must create a new String object to save the new content. The original string remains unchanged. This method is used because it is fixed and immutable strings are more efficient than variable strings. For the strings to be changed, there is a friend class of the string class called stringbuffer. Its object contains the strings that can be changed after creation. Both the string and stringbuffer classes are defined in Java. Lang. Therefore, they can be automatically used by all programs. Both are described as final, which means both do not include subclass. 2. String Constructor (1) string (), default constructor, no parameter string S1 = new string (); (2) string (char chars []), input character array char [] mychars = {'A', 'B', 'C'}; string S2 = new string (mychars) // use the string "ABC" to initialize S2 (3) string (char chars [], int startindex, int numchars) and input a character array, obtains the specified number of characters starting from the specified subscript position and uses these characters to initialize string variables. Char [] mychars = {'h', 'E', 'l', 'l', 'O'}; string S3 = new string (mychars, 1, 3 ); // use the string "ell" to initialize S3 (4) string (string strobj), pass in another string object, and use the content of this string object to initialize string S4 = new string (S3 ); // This is why S4 is also "ell. (5) string (byte asciichars []) string (byte asciichars [], int startindex, int numchars) Although Java's char type uses 16-bit (BIT) to represent the Unicode Character Set, on the Internet, the typical string format uses an 8-bit array consisting of the ASCII character set, because the 8-bit ASCII string is common. When a byte array is given, the string class provides the constructors of the above two initialization strings. Example: Package examples; Class substringconv {public static void main (string [] ARGs) {byte ASCII [] = {65,66, 67,68, 69,70 }; string S1 = new string (ASCII); system. out. println (S1); string S2 = new string (ASCII, 2, 3); system. out. println (S2) ;}} output after compilation and operation: abcdefcde 3. (1) Call the length () method of the string to obtain the length (number of characters) of the string. (2) this section describes how to use the new operator to create a string instance. However, this is an early method of handling string constants. For every string constant in the program, Java will automatically create a String object. Therefore, you can use a String constant to initialize a String object. Example: String S5 = "ABC"; int I = "ABC ". length (); // you can use strings as objects (3) You can use the "+" operator to connect two strings to generate a new String object. When the + operator has a string instance, the compiler converts the number of other operations to its string format. This operation is done by calling a String Conversion Method valueof () defined by the string class. For a simple type, the valueof () method returns a string that contains the value of this type. For objects, the valueof () method calls the tostring () method. Every class executes the tostring () method because it is defined by the object. The tostring () method has the following general form: String tostring (). The tostring () method returns a String object (that is, a string ). This object is generally a description of the class. 4. Some string operations (1) character truncation * char charat (INT where). Note that a char character is returned here, for example, char A; A = "ABCDE ". charat (2); // assign the index to 2, that is, the third character to a * void getchars (INT sourcestart, int sourceend, char target [], int targetstart ), here, there is no return value method. Specify the start and end subscript of the substring to be intercepted, then specify the array of Characters in the substring to be stored, and the start position of the characters to be stored. Note that the sub-string does not contain characters that are located at the end of the substring. * Byte [] getbytes () is the simplest form of the getbytes () method. It stores characters in byte arrays. Getbytes () is the most useful when outputting string values to an environment that does not support 16-bit Unicode encoding. For example, most Internet protocols and text file formats use 8-bit ASCII encoding during text exchange. * Char [] tochararray () converts all characters in a string to the simplest method of a character array. It can also be implemented using the getchars () method. (2) string comparison * Boolean equals (Object Str) compares two string objects to see if they are equal, Boolean inclusignorecasej (string Str) compares two string objects, and ignores the case of characters * regionmatches () the method allows you to compare the specified range in a string with the specified range in another string. The reload method allows you to ignore case sensitivity during comparison. The following describes the two methods: Boolean regionmatches (INT startindex, string str2, int str2startindex, int numchars) Boolean regionmatches (Boolean ignorecase, int startindex, string str2, int str2startindex, int numchars) Where startindex specifies the starting position of the substring in the string that calls this method. * The Boolean startswith (string Str) startswith method also has the second form: Boolean startwith (string STR, int startindex) Boolean endswith (string Str) the preceding method is used to determine whether a given string starts or ends with a specified string. * The Int compareto (string Str) method is used to compare the size of two strings. The result of string comparison and its meaning:
| Value |
Description |
| Less than 0 |
The string that calls this method is smaller than the STR Parameter |
| Greater than 0 |
The string that calls this method is greater than the STR Parameter |
| Equal to 0 |
The two strings are equal. |
(3) Other Operations * search string: int indexof (INT ch) int lastindexof (INT ch) int indexof (string Str) int lastindexof (string Str) Specify the start point of the Search: int indexof (int ch, int startindex) int lastindexof (int ch, int startindex) int indexof (string STR, int startindex) int lastindexof (string STR, int startindex) * use substring () to intercept a substring: string substring (INT startindex). Note that the substring in substring is not capitalized and the substring at the specified position is truncated. String substring (INT startindex, int endindex) truncates a substring that specifies the start position and end position. Note that the truncated character string does not include the character at the ending position. * Concat () connects to a string and performs the same functions as the + operator. String Concat (string Str) * replace () replaces the specified character in the specified string with another character: String Replace (char original, char replacement) for example: String S = "hello ". replace ('l', 'w'); // after execution, S = "hewwo"; * trim () returns a string, this string is the string obtained from the blank characters before and after the call string * string tolowercase () that changes the case of Characters in the string returns a string touppercase () with all letters in lowercase () returns a string of all uppercase letters. * use the valueof () method to convert data. For example, string STR = string. valueof (3); // converts the int type to the string type. Other basic data types and any class objects can also be used as parameters. Section 2: stringbufferstringbuffer defines the following constructor: stringbuffer () // default constructor, which reserves a space of 16 characters and does not need to be assigned stringbuffer (INT size) // set the specified buffer size stringbuffer (string Str) // set the content initialized by the stringbuffer object and reserve 16 characters, and do not need to allocate space * int length () call length () method to obtain the length of the stringbuffer object. Call capacity () to obtain the total allocation capacity. Both methods return an int value. * Void ensurecapacity (INT capacity) If you want to pre-allocate space for certain characters after constructing a stringbuffer object, you can use the ensurecapacity () method to set the buffer size, this is useful when it is known that a large number of small strings need to be appended to the stringbuffer. The ensurecapcity () method has the following general form: void ensurecapacity (INT capacity) * void setlength (INT Len) The setlength () method can be used to set the length of the stringbuffer object, the general format is as follows: void setlength (INT Len) If Len is greater than the current length () value of the stringbuffer object, a null character is added to the end of the stringbuffer object; if it is greater than length () if it is small, the strings after Len will be lost. * Char charat (INT where) void setcharat (INT where, char ch) the character at the specified position in the stringbuffer object can be obtained using the charat () method, setcharat () you can set characters at the specified position. Their general form is as follows: Char charat (INT where) void setcharat (INT where, char ch) for these two methods, the where value must be non-negative, it cannot exceed or equal to the length of the stringbuffer object. * The getchars (INT suorcestart, int sourceend, char target [], int targetstart) * append () method connects strings of any other data type to the end of the stringbuffer object, all built-in types and objects are reloaded. Below are several forms: stringbuffer append (string Str) stringbuffer append (INT num) stringbuffer append (Object OBJ) * insert () method inserts a string into another string. Below are several forms of stringbuffer insert (INT index, string Str) stringbuffer insert (INT index, char ch) stringbuffer insert (INT index, object OBJ) * reverse () // stringbuffer reverse () stringbuffer strbf = new stringbuffer ("abcdefg"); strbf. reverse (); system. out. println (strbf); // output gfedcba * stringbuffer Delete (INT startindex, int endindex) stringbuffer deletecharat (int loc) to delete strings and characters at the specified position. For example, delete all characters after the first character: strbf. delete (1, strbf. length (); * replace () is used to replace another string specifying the start position and end position in stringbuffer. Note that, the character to be replaced does not include the character at the ending position. Its general form is: stringbuffer Replace (INT startindex, int endindex, string Str) * substring () returns a part of stringbuffer value string substring (INT startindex) string substring (INT startindex, int endindex)