[Switch] experiences in using common Java Methods
Constantly improving ing...
Java. Lang. String split
The string split method is to directly split the string according to the given string, for example
Java code
- String value = "a, B, c, d, e ";
- String [] names = value. Split (",");
- For (INT I = 0, n = names. length; I
- System. Out. Print (Names [I]);
- }
Running result:
A B C D E
However, a problem occurs during IP resolution. The Code is as follows:
Java code
- String value = "209.242.1.1 ";
- String [] names = value. Split (".");
- For (INT I = 0, n = names. length; I
- System. Out. Print (Names [I] + "");
- }
The ideal output result should be 219 242 1, and nothing is output.
Very strange. Let's take a look at the split method signature.
Java code
- Public String [] Split (string RegEx)
The parameter name here is RegEx, that is, regular expression (regular expression ). This parameter is not a simple delimiter, but a regular expression:
Java code
- Public String [] Split (string RegEx, int limit ){
- Return Pattern. Compile (RegEx). Split (this, limit );
- }
The split method of the matcher class directly called by the split implementation. "." Has a special meaning in the regular expression, so we must escape it when using it.
The modification code is as follows:
Java code
- String value = "209.242.1.1 ";
- String [] names = value. Split ("//.");
- For (INT I = 0, n = names. length; I
- System. Out. Print (Names [I] + "");
- }
Replace Method
First, let's take a look at the introduction of the replace method.
Java code
- String java. Lang. String. Replace (char oldchar, char newchar)
- Returns a new string resulting from replacing all occurrences of oldchar in
- This string with newchar.
- If the character oldchar does not occur in the Character Sequence represented
- This string object, then a reference to this string object is returned. Otherwise,
- A New String object is created that represents a Character Sequence identical
- The character sequence represented by this string object, character t that every
- Occurrence of oldchar is replaced by an occurrence of newchar.
- Examples:
- "Mesquite in your cellar". Replace ('E', 'O ')
- Returns "mosquito in your collar"
- "The War of baronets". Replace ('R', 'y ')
- Returns "the way of bayonets"
- "Sparring with a purple porpoise". Replace ('P', 'T ')
- Returns "starring with a turtle tortoise"
- "Jonl". Replace ('Q', 'x') returns "jonl" (no change)
- Parameters:
- Oldchar the old character.
- Newchar the new character.
- Returns:
- A string derived from this string by replacing every occurrence of oldchar with newchar.
Usage:
Java code
- @ Test
- Public void testreplace (){
- String A = "AAA bcskd dkkaik kdafe ";
- System. Out. println (A. Replace ('A ','_'));
- }
Result: ___ bcskd dkkaik kd_fe
This method is used to replace char characters and cannot process strings.
A. Replace ('aaa', '=') is incorrect.
However, A. Replace ("AAA", "=") is acceptable.
Java code
- @ Test
- Public void testreplace (){
- String A = "AAA bcskd dkkaik kaaafe ";
- System. Out. println (A. Replace ("AAA", "= "));
- }
Result: = bcskd dkkaik = Fe
It can be seen that replace ("", "") and method replaceall ("", "") have similar functions
Take a closer look at the difference
Java code
- String java. Lang. String. Replace (charsequence target, charsequence replacement)
- Replaces each substring of this string that matches the literal target sequence
- With the specified literal replacement sequence. The replacement proceeds from
- The beginning of the string to the end, for example, replacing "AA" with "B" in
- The string "AAA" will result in "ba" rather than "AB ".
- Parameters:
- Target the sequence of char values to be replaced
- Replacement the replacement sequence of char values
- Returns:
- The resulting string
- Throws:
- Nullpointerexception if target or replacement is null.
- Since:
- 1.5
It's only available after 1.5.
But replaceall is more powerful.
Java code
- @ Test
- Public void testreplace (){
- String A = "AAA bcskd dkkaik kaaafe ";
- System. Out. println (A. Replace ("",""));
- }
The result is aaabcskddkkaikkaaafe.
Replaceall can remove spaces inside the string
However, you cannot use the Replace ('','') method.
The Replace ("", "") method is optional.
Another point
Java code
- @ Test
- Public void testreplace (){
- String A = "AAA bcskd dkkaik kaaafe ";
- System. Out. println (A. replaceall ("[A-Z]", "= "));
- }
Result:
Replaceall can use regular expressions. It's powerful.
Let's take a look at the usage of replaceall.
Java code
- String java. Lang. String. replaceall (string RegEx, string replacement)
- Replaces each substring of this string that matches the given regular expression
- With the given replacement.
- An invocation of this method of the form Str. replaceall (RegEx, REPL) yields exactly
- The same result as the expression
- Java. util. RegEx. pattern. Compile (RegEx). java. util. RegEx. pattern. matcher (Java. Lang. charsequence) matcher (STR). replaceall (repl)
- See also:
- Java. util. RegEx. Pattern
- Parameters:
- RegEx the regular expression to which this string is to be matched
- Returns:
- The resulting string
- Throws:
- Patternsyntaxexception if the regular expression's syntax is invalid
- Since:
- 1.4
- @ Spec
- JSR-51