Java learning review --- string practice and Stringbuffer, StringBuilder

Exercise 1 Remove the spaces at both ends of the string

Insert picture description here
The idea is that as long as the front is a space, the starting corner will move backward, and as long as the following is a space, the ending corner will move forward. When splitting the character string, pay attention to the terminating corner +1 at the end, because the segmentation will stop at the terminating corner, so the characters of the terminating corner will be missed. In order to avoid it, let the segmentation stop at the terminating corner +1.

Exercise 2: Reverse the string

Idea: Convert a string to an array. Reverse the array. Convert the array to a string.
Insert picture description here
If you want to achieve partial reversal, you only need to set two more parameters, namely, the start angle and the end angle.
Insert picture description here

Exercise 3: Get the number of occurrences of a string in a string.

Idea 1: Define a counter. Find the place where this string appears from the 0 subscript. If there is one, the counter will be incremented by one, and if not, it will be output as 0. Let the character string become a new character string from the end of the character string (the corner mark of the character string and the length of the character string can be calculated) to the end of the character string, and continue searching in the new character string. Loop until no string output is found.
Insert picture description here
Idea 2: Define a counter. Find the place where this string appears from the 0 subscript. If there is one, the counter will be incremented by one, and if not, it will be output as 0. Continue searching from the end of the string (calculated by adding the length of the string to the corner of the string). Loop until no string output is found.
Insert picture description here

Exercise 4

Insert picture description here

Insert picture description here

What the inner loop does: For example, the short string is "abcd", only abcd once in the first loop, then the inner loop ends, and the outer loop once. In the second inner loop, the start angle of the inner loop is still 0, but the end angle is already -1 at the end. The inner loop gets "abc" and "bcd" in turn to get all permutations of substrings. Then add one to the outer loop. In the third internal loop, the starting angle is marked as 0 and the ending angle is marked as -2 at the end. The inner loop sequentially gets "ab", "bc" and "cd". The loop continues in this way until the substring is contained by the long string, and the substring is output at this time, that is, the maximum identical substring of the two strings.

Stringbuffer

Insert picture description here
Stringbuffer is actually a container, which can be understood as a basin. No matter what is stuffed into it, this basin is still this basin, so there is no need to create a new object when stuffing this basin. New a stringbuffer is equivalent to creating a new basin.

storage

Insert picture description here
Insert picture description here

Insert picture description here

delete

Insert picture description here

Insert picture description here

Obtain

Insert picture description here

modify

Insert picture description here
Insert picture description here

Reverse

Insert picture description here

Store the specified content of the buffer into the specified position of the array

Insert picture description here
Insert picture description here

StringBuilder

StringBuilder is a substitute for
StringBuffer. It will be faster to use StringBuilder in single-threaded mode, but StringBuffer is still required in multi-threaded mode (with its own lock, and has better security). In multi-threaded mode, StringBuilder can also be used but you have to add it yourself The lock avoids safety issues.

Guess you like

Origin blog.csdn.net/weixin_46428711/article/details/111051084