Common methods of String in Java (ii)

Source: Internet
Author: User
Tags locale string methods

This article describes some of the remaining common String methods.

1. Replace method, Replacefirst method and ReplaceAll method

Replace (char OldChar, char Newchar)
Returns a string resulting from replacing all occurrences of oldChar in this string with Newchar.

Replace (charsequence target, charsequence replacement)
Replaces each substring of this string, matches the literal target sequence with the specified literal replacement seq Uence.

Replacefirst (string regex, string replacement)
Replaces the first substring of this string, the matches the given regular expression with the given replacement.

ReplaceAll (string regex, string replacement)
Replaces each substring of this string, matches the given regular expression with the given replacement.

Each of these methods replaces the corresponding contents of the original string with the specified character or string, the first two does not support regular expressions, and the back two supports regular

String x = "[Hello\\google\\bye]"; System.out.println (x.replace (' \ \ ', '/')); System.out.println (X.replace ("oo", "+ +")); System.out.println (X.replace ("\ \", "+ +")); System.out.println (X.replacefirst ("\\\\", "/")); System.out.println (X.replaceall ("oo", "+ +")); System.out.println (X.replaceall ("\\\\", "+ +"));

The output is:

[hello/google/bye][hello\g+gle\bye][hello++google++bye][hello/google\bye][ Hello\g+ +Gle\bye][hello++google++bye]

It is faster to test the ReplaceAll function. See source found, replace function inside still use ReplaceAll function.

General principle: The Replace function is recommended when the string cannot determine whether it has an escape character and does not need to be escaped; otherwise, use the ReplaceAll function.

PS: Why do I need four backslashes to match a backslash in a regular expression?

Parse the string "\\\\", the first and third are escape characters, the second and fourth are the slashes themselves.

It takes two slashes "\ \" to represent a slash in a string,

The slash inside the regular expression needs to be escaped, denoted by "\ \",

So we first want to express the inside of the regular expression "\ \", and then in a string representation, and these 2 slashes need an escape character, so that the 4 slash in the regular expression to represent a slash.

2. Matches method

Matches (String regex)
Tells whether or not this string matches the given regular expression.

This method is used to determine whether the string matches the given regular expression, which returns True, and false instead.

3. Split method

Split (String regex)
Splits this string around matches of the given regular expression.

Split (String regex, int limit)
Splits this string around matches of the given regular expression.

A string-splitting method that returns an array of type string.

The first argument int limit can limit the number of times a regular match is made,

If limit > 0, the number of regular matches is limit-1 times,

If limit < 0, the number of matches is not limited,

If limit = 0, the number of matches is not limited, and an empty string at the end of the delimited array is ignored.

String str = "Boo:and:foo"; System.out.println (Arrays.tostring (Str.split (":", 2)); System.out.println (Arrays.tostring (Str.split (":", 5)); System.out.println (Arrays.tostring (Str.split (":",-2))); System.out.println (Arrays.tostring (Str.split ("O", 5))); System.out.println (Arrays.tostring (Str.split ("O",-2))); System.out.println (Arrays.tostring (Str.split ("O", 0)));

The output is:

[Boo, And:foo] [Boo, and, Foo] [Boo, and, Foo] [B,,: And:f,,] [B,,: And:f,,] [B,,: And:f]

4. toLowerCase method and toUpperCase method

toLowerCase ()
Converts all of the characters in this String to lower case using the rules of the default locale.

toUpperCase ()
Converts all of the characters in this String to upper case using the rules of the default locale.

Uppercase and lowercase conversion methods

String str = "Hello google"= "Hello google"; System.out.println (Str1.tolowercase ());

The output is:

HELLO Googlehello Google

5. Trim method

Trim ()
Returns a string whose value is the this string, with any leading and trailing whitespace removed.

Remove whitespace before and after a given string

String str = "       Hello, Google              "; System.out.println (Str.trim ());

Output Result:

Hello, Google

6. ValueOf method

Converts other types of objects to strings, including Boolean,char,char[],double,float,int,long and the object type.

Returns the corresponding string or null.

Common methods of String in Java (ii)

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.