Common Java Methods: Split and replaceall

Source: Internet
Author: User

 

[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

  1. String value = "a, B, c, d, e ";
  2. String [] names = value. Split (",");
  3. For (INT I = 0, n = names. length; I
  4. System. Out. Print (Names [I]);
  5. }

Running result:

A B C D E

However, a problem occurs during IP resolution. The Code is as follows:

Java code

  1. String value = "209.242.1.1 ";
  2. String [] names = value. Split (".");
  3. For (INT I = 0, n = names. length; I
  4. System. Out. Print (Names [I] + "");
  5. }

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

  1. 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

  1. Public String [] Split (string RegEx, int limit ){
  2. Return Pattern. Compile (RegEx). Split (this, limit );
  3. }

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

  1. String value = "209.242.1.1 ";
  2. String [] names = value. Split ("//.");
  3. For (INT I = 0, n = names. length; I
  4. System. Out. Print (Names [I] + "");
  5. }

Replace Method

First, let's take a look at the introduction of the replace method.

Java code

  1. String java. Lang. String. Replace (char oldchar, char newchar)
  2. Returns a new string resulting from replacing all occurrences of oldchar in
  3. This string with newchar.
  4. If the character oldchar does not occur in the Character Sequence represented
  5. This string object, then a reference to this string object is returned. Otherwise,

  6. A New String object is created that represents a Character Sequence identical

  7. The character sequence represented by this string object, character t that every

  8. Occurrence of oldchar is replaced by an occurrence of newchar.
  9. Examples:
  10. "Mesquite in your cellar". Replace ('E', 'O ')
  11. Returns "mosquito in your collar"
  12. "The War of baronets". Replace ('R', 'y ')
  13. Returns "the way of bayonets"
  14. "Sparring with a purple porpoise". Replace ('P', 'T ')
  15. Returns "starring with a turtle tortoise"
  16. "Jonl". Replace ('Q', 'x') returns "jonl" (no change)
  17. Parameters:
  18. Oldchar the old character.
  19. Newchar the new character.
  20. Returns:
  21. A string derived from this string by replacing every occurrence of oldchar with newchar.

Usage:

Java code

  1. @ Test
  2. Public void testreplace (){
  3. String A = "AAA bcskd dkkaik kdafe ";
  4. System. Out. println (A. Replace ('A ','_'));
  5. }

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

  1. @ Test
  2. Public void testreplace (){
  3. String A = "AAA bcskd dkkaik kaaafe ";
  4. System. Out. println (A. Replace ("AAA", "= "));
  5. }

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

  1. String java. Lang. String. Replace (charsequence target, charsequence replacement)
  2. Replaces each substring of this string that matches the literal target sequence

  3. With the specified literal replacement sequence. The replacement proceeds from

  4. The beginning of the string to the end, for example, replacing "AA" with "B" in

  5. The string "AAA" will result in "ba" rather than "AB ".
  6. Parameters:
  7. Target the sequence of char values to be replaced
  8. Replacement the replacement sequence of char values
  9. Returns:
  10. The resulting string
  11. Throws:
  12. Nullpointerexception if target or replacement is null.
  13. Since:
  14. 1.5

It's only available after 1.5.

But replaceall is more powerful.

Java code

  1. @ Test
  2. Public void testreplace (){
  3. String A = "AAA bcskd dkkaik kaaafe ";
  4. System. Out. println (A. Replace ("",""));
  5. }

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

  1. @ Test
  2. Public void testreplace (){
  3. String A = "AAA bcskd dkkaik kaaafe ";
  4. System. Out. println (A. replaceall ("[A-Z]", "= "));
  5. }

Result:

Replaceall can use regular expressions. It's powerful.

Let's take a look at the usage of replaceall.

Java code

  1. String java. Lang. String. replaceall (string RegEx, string replacement)
  2. Replaces each substring of this string that matches the given regular expression

  3. With the given replacement.
  4. An invocation of this method of the form Str. replaceall (RegEx, REPL) yields exactly

  5. The same result as the expression
  6. Java. util. RegEx. pattern. Compile (RegEx). java. util. RegEx. pattern. matcher (Java. Lang. charsequence) matcher (STR). replaceall (repl)
  7. See also:
  8. Java. util. RegEx. Pattern
  9. Parameters:
  10. RegEx the regular expression to which this string is to be matched
  11. Returns:
  12. The resulting string
  13. Throws:
  14. Patternsyntaxexception if the regular expression's syntax is invalid
  15. Since:
  16. 1.4
  17. @ Spec
  18. JSR-51

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.