Using string __groovy in Groovy

Source: Internet
Author: User
Tags assert

Groovy supports two types of strings: A generic Java string, an instance of java.lang.String, and Gstrings, which is an instance of groovy.lang.GString, and allows the text to contain placeholders. Gstrings is not a subclass of string because the string class is the final class (final Class) that cannot be inherited. However, Gstring is the same as a generic string, because groovy can transform gstrings into Java strings.

Gstring is useful for writing template code (templating) because you must dynamically build a string (Gstrings are useful in templating situations where and have to build your Strin G dynamicly). The following list gives an example.

Use gstrings:

FirstWord = ' Hello ' Secondword = ' dlrow ' println ' $firstWord ${secondword.reverse ()} '

The results are as follows:

Hello World

Gstrings differs from the Java strings in that they allow ${.} The syntax of the embedded variable. If a string is enclosed in double or triple quotes and contains a non-escape character (unescaped) $, it must be an instance of groovy.lang.GString; otherwise, it is an instance of java.lang.String.

${..} Can contain any valid groovy expressions, including method calls or variable names. Only when the Gstring ToString method is invoked (for example, to output the result to the console), ${.} An expression is evaluated.

Another string that groovy supports is java.lang.String. However, GDK also dynamically injects many help methods to make them easy to apply.

The following example shows the different ways to declare a string in groovy:

S1 = "Hello \" world\ ""//escape double quotes, escape double quotes s2 = ' Hello ' world ' assertS1 = = S2 s3 = ' Hello \ ' world\ '//escape single quotes, escape apostrophe s4 = "Hello ' World" " assertS3 = = S4 assert NewString (' Hello world ') = = "Hello World" def s = [' h ', ' e ', ', ' l ', ' l ', ' o '] as Char[] assert NewString (s) = = ' Hello ' assert NewString (s,0,4) = = ' Hell ' s.eachwithindex{ch,index-> assertCH = = S[index]} assert' Hello '. ToCharArray () = = [' h ', ' e ', ' l ', ' l ', ' O ']

Common escape characters:

assert' t ' = = ' \011 '//tab,tab key assert' \ n ' = = ' \012 '//new line, newline character assert' \ r ' = = ' \015 '//carriage return, enter assert' \ n ' = ' "" "" "//spanning multiple lines, across multiple lines

String representation of the object:

def object = NewObject () assertString.valueof (object) = = Object.ToString ()//objects assertString.valueof ( true) == true. toString ()//booleans assertString.valueof (' a ') = = ' A '. toString ()//characters assertString.valueof (1) = = 1.toString ()//numbers assertString.valueof ([a:1,b:2]) = = [A:1,b:2].tostring ()//collections

String length:

s = "Hello World" assertS.size () = 11 assertS.size () = = S.length ()

Padding (PAD) string:

assert' Hello '. PadRight (7, ' * ') = = ' hello** ' assert' Hello '. PadLeft (7, ' * ') = = ' **hello ' assert' Hello '. Center (9, ' * ') = = ' **hello** '

Explode string:

s = "The quick brown fox jumps over the lazy dog" assertS.tokenize () = = [' The ', ' quick ', ' brown ', ' fox ', ' jumps ', ' over ', ' the ', ' lazy ', ' dog '] assertS.tokenize () = = NewStringTokenizer (s). Collect{it} S1 = "The,quick*brown*fox,*jumps*over*the*lazy*dog" assertS1.tokenize (', * ') = = S.tokenize ()

Query string:

Alphabets = NewString (' a '.. ' Z ' as Char[]) assertAlphabets.find{it > ' f '} = = ' G '//first one found assertAlphabets.findall{it > ' f '} = = ' G '. ' Z '//all found assertalphabets.findindexof{it > ' f '} = = Alphabets.indexof (' g ') assertalphabets.every {it > ' A '} assertAlphabets.any{it < ' C '} assertAlphabets.startswith (' abc ') assertAlphabets.endswith (' xyz ') assertAlphabets.contains (' Def ') assertAlphabets.indexof (' c ') = = 2 assertAlphabets.indexof (' d ') = = Alphabets.lastindexof (' d ') assertAlphabets.count (' a ') = = 1

Replace string:

s = "Hello" assertS.replace (' H ', ' Y ') = = ' Yello ' assertS.replace (' l ', ' p ') = = ' Heppo '

String inversion:

s = ' mirror ' assertS.reverse () = = ' Rorrim '

String operator:

assert' Hello ' + ' world '-' l ' = = ' helo world '//subtracts at most one L assert(' Is Sunday '-' Sunday '). Trim () = = ' is ' assert' Hello ' * 2 = ' Hello hello ' def empty = [] assert' abc '. Each{empty << It} = = ' abc ' assert' ABC '. Next () = = ' Abd ' assert' ABC '. Previous () = = ' ABB '

Subscript operator:

assert' Hello ' [1] = = ' E ' assert' Hello ' [2 ... ' Hello '. Size ()-1] = = ' Llo ' assert' Hello ' [0,2,4] = = ' Hlo ' assert' Hello ' [ -4,-2] = = ' El '

String comparisons:

assert' A ' < ' B ' assert' A ' > ' a ' assert' A '. CompareTo (' b ') = = 1 assert' A '. Comparetoignorecase (' a ') = = 0

Find the maximum and minimum values:

Collections.max (' AbcdeF '. ToList ()) = = ' E ' assertCollections.max (' AbcdeF '. ToList (), string.case_insensitive_order) = = ' F ' assertCollections.min ([' abc ', ' Abd ', ' abe ']) = = ' abc ' assertCollections.min ([' abc ', ' ABd ', ' AbE '], string.case_insensitive_order) = = ' abc '

Stringbuffers and stringbuilders are variable, allowing string changes. StringBuilder is not thread safe, so the processing speed is faster than stringbuffers.

def SB = NewStringBuffer (' Hello World ') assertsb.tostring () = = ' Hello World ' sb.length = 5 assertsb.tostring () = = ' Hello ' assert sb.substring (0,2)  ==  ' he '    //use <<, append (String) &

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.