String class and StringBuffer class in JAVA

1. String class

    According to the provisions of the Java language, the String class is an immutable Unicode character sequence, and its role is to implement a static string that cannot be changed. For example, concatenating two strings results in a new string without changing the original string. In fact, all changes to strings result in new strings being generated, not changing the original string.

The implementation of strings and arrays is very similar, and the position of the characters in the string is also indicated by the index number. The numbering starts from 0, the second character is numbered 1, and so on. If the number to be accessed is not within the legal range, the system will generate a StringIndexOutOfBoundsExecption exception. A compile error occurs if the value of index is not an integer.

In the following instances:

public class Main {
    public static void main(String[] args) {
        String str1 = "TCLab";
        String str2 = new String("TCLab");
        }}

method Function
String str1=”TCLab” Automatically create String instances with string constants.
String str2=new String("TCLab") Pass to constructor via String object or string constant.
public String(char value[]) Assign the entire character array to the String constructor.
public String(char value[], int offset, int count) Assign part of the character array to the String constructor, offset is the starting subscript, and count is the length of the subarray.

2. StringBuffer class

    The content of the object created by the String class cannot be changed, and the change of the string can only be achieved by creating a new string. If you want to add, modify, and delete the content of the string, you need to use the StringBuffer class. This class of object entity The memory space can be automatically resized to accommodate a variable sequence of characters.

   

Three constructors provided by the StringBuffer class
Construction method illustrate
StringBuffer() The StringBuffer object created using this parameterless construction method has an initial capacity of 16 characters. When the character sequence stored in the object is greater than 16 characters, the capacity of the object will increase automatically. The object can obtain the length of the character sequence stored in the entity through the length() method, and obtain the actual capacity of the current object through the capacity() method.
StringBuffer(int length) The StringBuffer object created by this construction method has the initial capacity of the number of characters specified by the parameter length. When the length of the character sequence stored by the object is greater than length, the capacity of the object is automatically increased to store the added characters.
StringBuffer(Strin str) The StringBuffer object created using this constructor has an initial capacity of the length of the parameter string str plus 16 characters.


Strings can be manipulated through the following methods provided by the StringBuffer class:

Several commonly used methods of StringBuffer class
method illustrate
append() Use the append() method to convert other Java type data into strings and then append them to the StringBuffer object.
insert(int index, String str) The insert() method inserts a string into the object's character sequence at a position.
setCharAt (int n, char ch) Replace the character at the character sequence n in the current StringBuffer object with the character specified by the parameter ch. The value of n must be non-negative and less than the length of the string sequence in the current object.
reverse() Use the reverse() method to reverse the sequence of characters in an object.
delete(int n, int m) Removes a sub-character sequence from the character sequence in the current StringBuffer object. Here n specifies the subscript of the first character to be deleted, and m specifies the subscript of the next character after the last character to be deleted, so the substring to be deleted is from n~m-1.
replace(int n, int m, String str) Replaces the sequence of characters in the object with str, the sub-character sequence being replaced by the subscripts n and m.
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324706166&siteId=291194637