Guava Learning notes: manipulating strings in guava

Source: Internet
Author: User
Tags google guava

Guava Learning notes: manipulating strings in guava

Reprint: Http://outofmemory.cn/java/guava/base/Strings

In Google Guava, it provides a great convenience for string manipulation, with an established judgment string whether it is an empty string or null, fills a string with a specified character, splits a merged string, determines the string match, and so on.

Let's look at each of these actions:

1. Use the Com.google.common.base.Strings class's IsNullOrEmpty (input) method to determine if the string is empty

Strings.isnullorempty (Input) DemoStringInput= ""; boolean IsNullOrEmpty =strings.inputsystem.. Println ( "input"  + Span class= "pun" > (isnullorempty? " Is ":" are not ")  +  "null or empty."                

2. Get the same prefix or suffix for two strings

Strings.commonprefix (A, B) demoStringA= "Com.jd.coo.Hello";StringB= "Com.jd.coo.Hi";StringOurcommonprefix= Strings.Commonprefix(A,B);System.Out.println("A, b common prefix is" +Ourcommonprefix);Strings.commonsuffix (A, B) demoStringC= "Com.google.Hello"; string D =  " Com.jd.Hello ";string Oursuffix =  strings. Commonsuffix (c,d        system.. Println ( "c,d common suffix is"  + Oursuffix             

3. Strings Padstart and Padend methods to complement the full string

IntMinLength= 4;StringPadendresult= Strings.Padend("123",MinLength, ' 0 ');System.Out.println("Padendresult is" + Padendresultstring Padstartresult = strings. Padstart ( "1" , 2,  ' 0 ' ); system.. Println ( "Padstartresult is"  +  Padstartresult          

4. Use the splitter class to split a string

Splitter class can be conveniently based on regular expressions to split the string, you can remove the split results in the empty string, can be split after the string trim operation, you can do two times split.

Let's look at a basic split example:

Iterable<String>Splitresults= Splitter.Onpattern("[,,]{1,}").Trimresults().Omitemptystrings ()                 .split (for  (string< Span class= "PLN" > item : Splitresults)  Span class= "pun" >{            system out. Printlnitem           

The splitter Onpattern method passes in a regular expression, followed by the Trimresults () method that indicates that the result is to be trim,omitemptystrings () to omit the empty string, and the split method performs a split operation.

Split returns a result of iterable<string> we can use the FOR Loop statement to print the result of a split string individually.

Splitter also has a more powerful function, doing two splits, here two splits mean split two times, for example we can split a string such as A=b;c=d into a map<string,string>.

StringTosplitstring= "A=b;c=d,e=f";Map<String,String>Kvs= Splitter.Onpattern("[,;] {1,} ").Withkeyvalueseparator(=).Split(Tosplitstring);For (Map.Entry<String,String> entry : Kvs.entryset ())  {            system.. Println (string. Format ( "%s=%s" , Entry . getkey (), entry.        }          

Two splits first by using Onpattern to do the first split, and then by the Withkeyvalueseperator (") method to do the second split.

5. There is a split string bound to have a merged string, guava provides us with the Joiner class to do the merging of strings

Let's look at a simple example:

        string Joinresult = joiner.on ( (new string[]{< span class= "str" > "hello" , "world" }); system.. Printlnjoinresult       

In the example above we use Joiner.on (""). Join (XX) to merge strings. It's very simple and effective.

The splitter method can do two splits on the string, and the corresponding joiner can be reversed, merging the map<string,string>. Let's look at the following example:

Map<String,String>Map= New HashMap<String,String> ();Map.Put(A, "B");Map.Put "C" , "D" string Mapjoinresult = joiner. "="  Joinkvssystem.. Printlnmapjoinresult        

Use the Withkeyvalueseparator method to merge the map. The result of the merge is:a=b,c=d

In the guava library, you can also make a case conversion (Caseformat enumeration) of strings, and you can do pattern matching on strings. It is very convenient to use, it is not introduced.

Guava Learning notes: manipulating strings in guava

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.