String buffer StringBuffer Class
Lookup StringBuffer's Api,stringbuffer, also known as the variable character sequence, is a string-like buffer that can be changed by some method calls to the length and content of the sequence.
The original StringBuffer is a string buffer, that is, it is a container, the container can be loaded with a lot of strings. and the ability to perform various operations on the strings .
StringBuffer base is dependent on a character array to store character data, the default initial capacity of this character array is 16,
If the length of the character array is not enough, it automatically grows 1 time times +2.
StringBuffer: This class is used to create variable string objects that allow strings to be inserted, deleted, modified, and so on after they are created. And the StringBuffer class is a string buffer class, and each of his objects has an initial capacity, and the default is 16 characters in length. As long as the characters in it do not exceed its capacity, there is no need to allocate new internal buffer capacity, or it will automatically increase.
Characteristics:
1: You can modify the contents of the string.
2: is a container.
3: Is variable-length.
4: Any type of data can be stored in the buffer.
5: Eventually it needs to become a string.
the StringBuffer method uses
append (String str) appends the specified string to this sequence of characters
Delete (int start, int end) moves out the characters in the substring of this sequence
Insert (int offset, string str) inserts a string into this sequence of characters
Replace (int start, int end, String str) replaces the characters in this sequence substring with the characters in the given string
reverse () replaces this sequence of characters with a reversed form
toString () returns the string representation of the data in this sequence
New StringBuffer (); Sb.append (// Add string sb.insert (2, "it"); // Insert Sb.delete (1, 4) at the specified position ; // Delete sb.replace (1, 4, "cast"); // Replace content within a specified range String str = sb.tostring ();
Note: After the Append, delete, insert, replace, and reverse method calls,
The return value is the current object itself, so to speak, stringbuffer it can change the length and content of the character sequence.
Method chaining calls to objects
In our development, we encounter the case of returning an object after invoking a method. It then uses the returned object to continue calling the method. At such times, we can write the code together, like the Append method, with the following code:
Creates a string buffer object. Used to store data.
StringBuffer sb = new StringBuffer ();
Add data. After constantly adding data, to manipulate the last data of the buffer, it must be converted to a string.
String str = Sb.append (True). Append ("hehe"). ToString ();
No matter how much data, what type of data is not important, as long as the end becomes a string can use StringBuffer this container
StringBuilder class
StringBuilder analogy StringBuffer class to be fast, but not secure
Zheng-----table------
A regular expression is a string that is described with a single character and used to define a matching rule that matches a series of strings that conform to a certain syntactic rule
Often used in development to retrieve text that matches a rule
Matching rules:
Clear case sensitivity
Character: X
Represents the character X
Character: \ \
Indicates "\"
Character: \ t
Tabs
Character: \ n
Line break
Character: \ r
Carriage return character
Character class: [ABC]
Indicates matching A or B or C
Character class: [^ABC]
Matches any character except A or B or C
Character class: [A-za-z]
Represents a to Z or A to Z, and the letters at both ends are included
Character class: [0-9]
Numbers that represent 0 to 9 include two digits
Character class: [A-za-z_0-9]
Represents the need to match letters or underscores or numbers
Predefined character class: ". "
The matching rule is ".", then a match is required for an arbitrary character. If you want to use it. , use the matching rule "\ \." To achieve
Predefined character classes: \d
Meaning: represents 0 to 9 digits, both ends of the number are included, equivalent to [0-9]
Predefined character classes: \w
Represents matching letters or numbers or underscores
Boundary match: ^
Meaning: Represents the beginning of a line
For example, if the matching rule is ^[abc][0-9]$ , then the content that needs to be matched starts at [ABC], which is equivalent to the left double quotation mark
Boundary Match: $
Meaning: Represents the end of a line
For example, if the matching rule is ^[abc][0-9]$ , then the match must end with [0-9], which is equivalent to the right double quotation mark
Boundary Matching device: \b
Meaning: The word boundary is represented
For example: The match rule is "\b[abc]\b" , which means that the right and left sides of the letter A or B or C need non-word characters ([a-za-z_0-9])
Quantity Word: X?
Meaning: X appears once or once or no
For example: The match rule is "A?", then the content that needs to be matched is a character a, or a does not have
Quantity Word: x*
Meaning: X appears 0 or more times
For example, if the match rule is "A *" , then the content that needs to be matched is multiple characters A, or a does not
Quantity Word: x+
Meaning: X appears one or more times
For example: The match rule is "A +", then the content that needs to be matched is more than one character a, or a
Quantity Word: X{n}
Meaning: The x appears exactly n times
For example: The match rule is "a{5}", then the content that needs to match is 5 characters a
Quantity Word: X{n,}
What it means: X appears at least n times
For example: The match rule is "a{5,}", then the content that needs to be matched is a minimum of 5 characters a
Quantity Word: X{n,m}
Meaning: X appears at least n times, but not more than M
For example: The match rule is "a{5,8}", then the content that needs to match is 5 characters A to 8 characters a
Practice:
Matching rules:
Match positive integer: "\\d+"
Match positive decimals: "\\d+\\.\\d+"
Match negative integer: "-\\d+"
Match negative decimals: "-\\d+\\.\\d+"
Match a positive number that retains two decimal places: "\\d+\\.\\d{2}"
Match a positive number that retains 1-3 decimal places: "\\d+\\.\\d{1,3}"
L match a legitimate mailbox:
"[A-za-z_0-9][email protected][a-za-z_0-9]+ (\\.[ a-za-z_0-9]+) + "
"\\[email protected]\\w+ (\\.\\w+) +"
Common methods in string classes involving regular expressions
A boolean matches (string regex) tells whether this string matches the given regular expression Boolean
string[] Split (string regex) splits this string according to the given regular expression
string ReplaceAll(string regex, stringreplacement) replaces this string with the given replacement for all substrings that match the given regular expression
L public boolean matches(string regex)//Determine if a string matches a given rule
Example: Check the QQ number.
1: The requirement must be 5-15 digits
2:0 cannot begin
Code Demo:
String qq = "604154942";
String regex = "[1-9][0-9]{4,14}";
boolean Flag2 = qq.matches (regex);
Example: Verifying a mobile phone number
1: Required for 11 digits
2: The 1th bit is 1, the 2nd bit is 3, 4, 5, 7, 8, and the next 9 bits are any number between 0 and 9.
Code Demo:
String phone = "18800022116";
String regex = "1[34578][0-9]{9}";
boolean flag = phone.matches (regex);
L Public string[] split(string regex)//split this string based on a matching rule for a given regular expression
Example: Splitting a number in a string
Code Demo:
String s = "18-22-40-65";
String regex = "-";
String[] result = S.split (regex);
Code Demo:
String s = "18 22 40 65";
String regex = "";
String[] result = S.split (regex);
L public string replaceall(string regex,string Replacement)//replace the string contents of the rule with the new string
Example: replacing numbers in text with *
Code Demo:
String s = "hello12345world6789012";
String regex = "[0-9]";
String result = S.replaceall (regex, "*");
java-Basics-Common APIs (StringBuffer class, regular expressions)