1, StringBuilder class
1. Difference from String
The StringBuilder class is similar to the String class. The main difference between the two is that you can change the contents of the StringBuilder object, but you cannot change the contents of the String object. Recall that String objects are immutable. This means that once the content of the String object is set, the String value it saves cannot be changed.
Take an example:
String name; name = "George"; name = "Sally";
When the name variable here changes from George to Sally, it does not change its value, but directly assigns a new object Sally to the name variable.
Unlike string, StringBuilder can change, insert and delete specific characters in a string. For an object of type stringbulider, it can change the length of the string, which can improve the efficiency of the program compared with the string class.
2. Constructor
StringBuilder city = new StringBuilder("Charleston"); System.out.println(city);
This code creates a StringBuilder object and assigns its address to the city variable. The object is initialized with the string "Charleston". As shown in this code, we can pass the string generator object to the println and print methods. One limitation of the StringBuilder class is that the object of the string class cannot be directly assigned to it:
StringBuilder city = "Charleston"; // ERROR!!! Will not work!
We must use a new statement and call the constructor to create a StringBuilder object.
3. Some methods
The StringBuilder class provides many methods similar to string:
char charAt(int position) void getChars(int start, int end, char[] array, int arrayStart) int indexOf(String str) int indexOf(String str, int start) int lastIndexOf(String str) int lastIndexOf(String str, int start) int length() String substring(int start) String substring(int start, int end)
Of course, the StringBuilder class also provides some methods that string does not have.
(1) append method
The StringBuilder class has a method called append, which has several overloaded versions. These methods accept a parameter, which can be any original data type, char array and string object. They add the string representation of their parameters to the end of the calling object.
StringBuilder str = new StringBuilder(); // Append values to the object. str.append("We sold "); // Append a String object. str.append(12); // Append an int. str.append(" doughnuts for $"); // Append another String. str.append(15.95); // Append a double. // Display the object's contents. System.out.println(str);
(2) insert method
The StringBuilder class also has several methods called insser, and it also has several overloaded versions, which insert values into the called object. These methods accept two parameters: an int that specifies where the insertion should start; The other is the content to insert. The content to insert can be any raw data type, char array, or string object.
StringBuilder str = new StringBuilder("New City"); str.insert(4, "York "); System.out.println(str);
The first line creates a StringBuilder object initialized with the string "New City". The second statement inserts the string "York" after the fourth character of the StringBuilder object, that is, after the space. All characters after position 4 in the current object are moved to the right. In memory, the StringBuilder object automatically expands its size to accommodate the inserted characters. If we run this program, New York City will be printed.
Take another example:
char cArray[] = { '2', '0', ' ' }; StringBuilder str = new StringBuilder("In July we sold cars."); str.insert(16, cArray); System.out.println(str);
(3) Methods related to deletion
StringBuilder str = new StringBuilder("I ate 100 blueberries!"); // Display the StringBuilder object. System.out.println(str); // Delete the '0'. str.deleteCharAt(8); // Delete "blue". str.delete(9, 13); // Display the StringBuilder object. System.out.println(str); // Change the '1' to '5' str.setCharAt(6, '5'); // Display the StringBuilder object. System.out.println(str);
(4) toString method
StringBuilder strb = new StringBuilder("This is a test."); String str = strb.toString();
2, Tokenizing Strings
First look at the following strings:
These strings have their separators (space, semicolon; -; /), and the string is divided into a small string according to the separator, which is token strings. The string class has a split method to perform such operations.
/** This program demonstrates the String class's split method. */ public class SplitDemo1 { public static void main(String[] args) { // Create a string to tokenize. String str = "one two three four"; // Get the tokens, using a space delimiter. String[] tokens = str.split(" "); // Display the tokens. for (String s : tokens) System.out.println(s); } }
In the previous example, we passed a space to the split method, which specifies that the space is a delimiter. The split method also allows us to use multi character separators. This means that we are not limited to a single character as a separator. For example, the separator can be a complete word:
// Create a string to tokenize. String str = "one and two and three and four"; // Get the tokens, using " and " as the delimiter. String[] tokens = str.split(" and "); // Display the tokens. for (String s : tokens) System.out.println(s);
The previous code demonstrates a multi character separator (a separator that contains multiple characters). We can also specify a series of characters, each of which is a separator. For example, we want @ and Are separators, which can be put in parentheses: "[@.]"
// Create a string to tokenize. String str = "joe@gaddisbooks.com"; // Get the tokens, using @ and . as delimiters. String[] tokens = str.split("[@.]"); // Display the tokens. for (String s : tokens) System.out.println(s);
The string class also has a method for removing whitespace, called trim:
// Create a string with leading and trailing whitespaces. String str = " one;two;three "; // Trim leading and trailing whitespace. str = str.trim(); // Tokenize the string using the semicolon as a delimiter. String[] tokens = str.split(";"); // Display the tokens. for (String s : tokens) { System.out.println("*" + s + "*"); }
Run the program and print:
instead of: