在Groovy中使用字符串
Groovy支持两种类型字符串:一般的Java字符串,它是java.lang.String的实例;和GStrings,它是groovy.lang.GString的实例,并且允许文本中包含占位符。GStrings并不是String的子类,因为String类是最终类(final class)不能被继承。然而,GString与一般的字符串一样,因为Groovy能将GStrings转型为Java strings。
GString 适用于编写模板代码(templating),因为您必须动态构建字符串(GStrings are useful in templating situations where you have to build your string dynamicly)。以下的清单给出了一个例子。
使用GStrings:
<ol class="dp-j"> <li class="alt"><span><span>firstWord = </span><span class="string"><font color="#0000ff">'Hello'</font></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span>secondWord = </span><span class="string"><font color="#0000ff">'dlroW'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>println </span><span class="string"><font color="#0000ff">"$firstWord ${secondWord.reverse()}"</font></span><span> </span> </li> </ol>
结果如下:
<ol class="dp-xml"><li class="alt"><span><span>Hello World </span></span></li></ol>
GStrings不同于Java strings在于它们允许以${..}的语法嵌入变量。如果一个字符串由双引号或三重引号括起来,并且包含一个非转义符(unescaped)$,它必定是groovy.lang.GString的一个实例;否则,就是java.lang.String的一个实例。
${..}里可以包含任意有效的Groovy表达式,包括方法调用或变量名。只有调用GString的toString方法时(例如,把结果输出到控制台),${..}表达式才被求值。
Groovy支持的另一种字符串便是java.lang.String。然而,GDK还是动态注入许多帮助方法,使其易于应用。
以下的例子展示了在Groovy中声明一个字符串的不同方式:
<ol class="dp-j"> <li class="alt"><span><span>s1 = </span><span class="string"><font color="#0000ff">"Hello \"World\" "</font></span><span> </span><span class="comment"><font color="#008200">//Escape double quotes,转义双引号 </font></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span>s2 = </span><span class="string"><font color="#0000ff">'Hello "World" '</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> s1 == s2 </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>s3 = </span><span class="string"><font color="#0000ff">'Hello \'World\' '</font></span><span> </span><span class="comment"><font color="#008200">//Escape single quotes,转义单引号 </font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>s4 = </span><span class="string"><font color="#0000ff">"Hello 'World' "</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> s3 == s4 </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> String(</span><span class="string"><font color="#0000ff">'Hello World'</font></span><span>) == </span><span class="string"><font color="#0000ff">"Hello World"</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>def s = [</span><span class="string"><font color="#0000ff">'h'</font></span><span>,</span><span class="string"><font color="#0000ff">'e'</font></span><span>,</span><span class="string"><font color="#0000ff">'l'</font></span><span>,</span><span class="string"><font color="#0000ff">'l'</font></span><span>,</span><span class="string"><font color="#0000ff">'o'</font></span><span>] as </span><span class="keyword"><strong><font color="#006699">char</font></strong></span><span>[] </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> String(s) == </span><span class="string"><font color="#0000ff">'hello'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> String(s,</span><span class="number"><font color="#c00000">0</font></span><span>,</span><span class="number"><font color="#c00000">4</font></span><span>) == </span><span class="string"><font color="#0000ff">'hell'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>s.eachWithIndex{ch,index -> </span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> ch == s[index]} </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'hello'</font></span><span>.toCharArray() == [</span><span class="string"><font color="#0000ff">'h'</font></span><span>,</span><span class="string"><font color="#0000ff">'e'</font></span><span>,</span><span class="string"><font color="#0000ff">'l'</font></span><span>,</span><span class="string"><font color="#0000ff">'l'</font></span><span>,</span><span class="string"><font color="#0000ff">'o'</font></span><span>] </span> </li> <li class=""><span> </span></li> <li class="alt"><span> </span></li> </ol>
常用的转义符:
<ol class="dp-j"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'\t'</font></span><span> == </span><span class="string"><font color="#0000ff">'\011'</font></span><span> </span><span class="comment"><font color="#008200">//Tab,Tab键 </font></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'\n'</font></span><span> == </span><span class="string"><font color="#0000ff">'\012'</font></span><span> </span><span class="comment"><font color="#008200">//New line,换行符 </font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'\r'</font></span><span> == </span><span class="string"><font color="#0000ff">'\015'</font></span><span> </span><span class="comment"><font color="#008200">//Carriage return,回车 </font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'\n'</font></span><span> == </span><span class="string"><font color="#0000ff">""</font></span><span>" </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="string"><font color="#0000ff">""</font></span><span>" </span><span class="comment"><font color="#008200">//Spanning multiple lines,跨越多行 </font></span><span> </span> </li> </ol>
对象的字符串表示:
<ol class="dp-j"> <li class="alt"><span><span>def object = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> Object() </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> String.valueOf(object) == object.toString() </span><span class="comment"><font color="#008200">//Objects </font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> String.valueOf(</span><span class="keyword"><strong><font color="#006699">true</font></strong></span><span>) == </span><span class="keyword"><strong><font color="#006699">true</font></strong></span><span>.toString() </span><span class="comment"><font color="#008200">//Booleans </font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> String.valueOf(</span><span class="string"><font color="#0000ff">'a'</font></span><span>) == </span><span class="string"><font color="#0000ff">'a'</font></span><span>.toString() </span><span class="comment"><font color="#008200">//Characters </font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> String.valueOf(</span><span class="number"><font color="#c00000">1</font></span><span>) == </span><span class="number"><font color="#c00000">1</font></span><span>.toString() </span><span class="comment"><font color="#008200">//Numbers </font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> String.valueOf([a:</span><span class="number"><font color="#c00000">1</font></span><span>,b:</span><span class="number"><font color="#c00000">2</font></span><span>]) == [a:</span><span class="number"><font color="#c00000">1</font></span><span>,b:</span><span class="number"><font color="#c00000">2</font></span><span>].toString() </span><span class="comment"><font color="#008200">//Collections </font></span><span> </span> </li> </ol>
字符串长度:
<ol class="dp-j"> <li class="alt"><span><span>s = </span><span class="string"><font color="#0000ff">"Hello World"</font></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> s.size() == </span><span class="number"><font color="#c00000">11</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> s.size() == s.length() </span> </li> </ol>
填充(pad)字符串:
<ol class="dp-j"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'Hello'</font></span><span>.padRight(</span><span class="number"><font color="#c00000">7</font></span><span>,</span><span class="string"><font color="#0000ff">'*'</font></span><span>) == </span><span class="string"><font color="#0000ff">'Hello**'</font></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'Hello'</font></span><span>.padLeft(</span><span class="number"><font color="#c00000">7</font></span><span>,</span><span class="string"><font color="#0000ff">'*'</font></span><span>) == </span><span class="string"><font color="#0000ff">'**Hello'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'Hello'</font></span><span>.center(</span><span class="number"><font color="#c00000">9</font></span><span>,</span><span class="string"><font color="#0000ff">'*'</font></span><span>) == </span><span class="string"><font color="#0000ff">'**Hello**'</font></span><span> </span> </li> </ol>
分解字符串:
<ol class="dp-j"> <li class="alt"><span><span>s = </span><span class="string"><font color="#0000ff">"The quick brown fox jumps over the lazy dog"</font></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> s.tokenize() == [</span><span class="string"><font color="#0000ff">'The'</font></span><span>,</span><span class="string"><font color="#0000ff">'quick'</font></span><span>,</span><span class="string"><font color="#0000ff">'brown'</font></span><span>,</span><span class="string"><font color="#0000ff">'fox'</font></span><span>,</span><span class="string"><font color="#0000ff">'jumps'</font></span><span>,</span><span class="string"><font color="#0000ff">'over'</font></span><span>,</span><span class="string"><font color="#0000ff">'the'</font></span><span>,</span><span class="string"><font color="#0000ff">'lazy'</font></span><span>,</span><span class="string"><font color="#0000ff">'dog'</font></span><span>] </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> s.tokenize() == </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> StringTokenizer(s).collect{it} </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>s1 = </span><span class="string"><font color="#0000ff">"The,quick*brown*fox,*jumps*over*the*lazy*dog"</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> s1.tokenize(</span><span class="string"><font color="#0000ff">',*'</font></span><span>) == s.tokenize() </span> </li> </ol>
查询字符串:
<ol class="dp-j"> <li class="alt"><span><span>alphabets = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> String(</span><span class="string"><font color="#0000ff">'a'</font></span><span> .. </span><span class="string"><font color="#0000ff">'z'</font></span><span> as </span><span class="keyword"><strong><font color="#006699">char</font></strong></span><span>[]) </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> alphabets.find{it > </span><span class="string"><font color="#0000ff">'f'</font></span><span>} == </span><span class="string"><font color="#0000ff">'g'</font></span><span> </span><span class="comment"><font color="#008200">//First one found </font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> alphabets.findAll{it > </span><span class="string"><font color="#0000ff">'f'</font></span><span>} == </span><span class="string"><font color="#0000ff">'g'</font></span><span> .. </span><span class="string"><font color="#0000ff">'z'</font></span><span class="comment"><font color="#008200">//ALl found </font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> alphabets.findIndexOf{ it > </span><span class="string"><font color="#0000ff">'f'</font></span><span>} == alphabets.indexOf(</span><span class="string"><font color="#0000ff">'g'</font></span><span>) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> alphabets.every {it > </span><span class="string"><font color="#0000ff">'A'</font></span><span>} </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> alphabets.any{it <span class="string"><font color="#0000ff">'c'</font></span><span>} </span></span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> alphabets.startsWith(</span><span class="string"><font color="#0000ff">'abc'</font></span><span>) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> alphabets.endsWith(</span><span class="string"><font color="#0000ff">'xyz'</font></span><span>) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> alphabets.contains(</span><span class="string"><font color="#0000ff">'def'</font></span><span>) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> alphabets.indexOf(</span><span class="string"><font color="#0000ff">'c'</font></span><span>) == </span><span class="number"><font color="#c00000">2</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> alphabets.indexOf(</span><span class="string"><font color="#0000ff">'d'</font></span><span>) == alphabets.lastIndexOf(</span><span class="string"><font color="#0000ff">'d'</font></span><span>) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> alphabets.count(</span><span class="string"><font color="#0000ff">'a'</font></span><span>) == </span><span class="number"><font color="#c00000">1</font></span><span> </span> </li> </ol>
替换字符串:
<ol class="dp-j"> <li class="alt"><span><span>s = </span><span class="string"><font color="#0000ff">"Hello"</font></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> s.replace(</span><span class="string"><font color="#0000ff">'H'</font></span><span>,</span><span class="string"><font color="#0000ff">'Y'</font></span><span>) == </span><span class="string"><font color="#0000ff">'Yello'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> s.replace(</span><span class="string"><font color="#0000ff">'l'</font></span><span>,</span><span class="string"><font color="#0000ff">'p'</font></span><span>) == </span><span class="string"><font color="#0000ff">'Heppo'</font></span><span> </span> </li> </ol>
字符串反转:
<ol class="dp-j"> <li class="alt"><span><span>s = </span><span class="string"><font color="#0000ff">'mirror'</font></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> s.reverse() == </span><span class="string"><font color="#0000ff">'rorrim'</font></span><span> </span> </li> </ol>
字符串操作符:
<ol class="dp-j"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'hello'</font></span><span> + </span><span class="string"><font color="#0000ff">' world'</font></span><span> - </span><span class="string"><font color="#0000ff">'l'</font></span><span> == </span><span class="string"><font color="#0000ff">'helo world'</font></span><span> </span><span class="comment"><font color="#008200">//Subtracts at most one l </font></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> (</span><span class="string"><font color="#0000ff">'Today is Sunday'</font></span><span> - </span><span class="string"><font color="#0000ff">'Sunday'</font></span><span>).trim() == </span><span class="string"><font color="#0000ff">'Today is'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'hello '</font></span><span> * </span><span class="number"><font color="#c00000">2</font></span><span> == </span><span class="string"><font color="#0000ff">'hello hello '</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"><span>def empty = [] </span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'abc'</font></span><span>.each{empty <span class="string"><font color="#0000ff">'abc'</font></span><span> </span></span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'abc'</font></span><span>.next() == </span><span class="string"><font color="#0000ff">'abd'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'abc'</font></span><span>.previous() == </span><span class="string"><font color="#0000ff">'abb'</font></span><span> </span> </li> </ol>
下标操作符:
<ol class="dp-j"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'hello'</font></span><span>[</span><span class="number"><font color="#c00000">1</font></span><span>] == </span><span class="string"><font color="#0000ff">'e'</font></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'hello'</font></span><span>[</span><span class="number"><font color="#c00000">2</font></span><span>..</span><span class="string"><font color="#0000ff">'hello'</font></span><span>.size() - </span><span class="number"><font color="#c00000">1</font></span><span>] == </span><span class="string"><font color="#0000ff">'llo'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'hello'</font></span><span>[</span><span class="number"><font color="#c00000">0</font></span><span>,</span><span class="number"><font color="#c00000">2</font></span><span>,</span><span class="number"><font color="#c00000">4</font></span><span>] == </span><span class="string"><font color="#0000ff">'hlo'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'hello'</font></span><span>[-</span><span class="number"><font color="#c00000">4</font></span><span>,-</span><span class="number"><font color="#c00000">2</font></span><span>] == </span><span class="string"><font color="#0000ff">'el'</font></span><span> </span> </li> </ol>
字符串比较:
<ol class="dp-j"> <li class="alt"><span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'a'</font></span><span> <span class="string"><font color="#0000ff">'b'</font></span><span> </span></span><li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'a'</font></span><span> > </span><span class="string"><font color="#0000ff">'A'</font></span><span> </span> </li></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'a'</font></span><span>.compareTo(</span><span class="string"><font color="#0000ff">'b'</font></span><span>) == -</span><span class="number"><font color="#c00000">1</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> </span><span class="string"><font color="#0000ff">'a'</font></span><span>.compareToIgnoreCase(</span><span class="string"><font color="#0000ff">'A'</font></span><span>) == </span><span class="number"><font color="#c00000">0</font></span><span> </span> </li> </ol>
寻找最大值和最小值:
<ol class="dp-j"> <li class="alt"><span><span>Collections.max(</span><span class="string"><font color="#0000ff">'abcdeF'</font></span><span>.toList()) == </span><span class="string"><font color="#0000ff">'e'</font></span><span> </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> Collections.max(</span><span class="string"><font color="#0000ff">'abcdeF'</font></span><span>.toList(), String.CASE_INSENSITIVE_ORDER) == </span><span class="string"><font color="#0000ff">'F'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> Collections.min([</span><span class="string"><font color="#0000ff">'abc'</font></span><span>,</span><span class="string"><font color="#0000ff">'abd'</font></span><span>,</span><span class="string"><font color="#0000ff">'abe'</font></span><span>]) == </span><span class="string"><font color="#0000ff">'abc'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> Collections.min([</span><span class="string"><font color="#0000ff">'Abc'</font></span><span>,</span><span class="string"><font color="#0000ff">'aBd'</font></span><span>,</span><span class="string"><font color="#0000ff">'abE'</font></span><span>], String.CASE_INSENSITIVE_ORDER) == </span><span class="string"><font color="#0000ff">'Abc'</font></span><span> </span> </li> </ol>
StringBuffers和StringBuilders是可变的,允许字符串改变。StringBuilder不是线程安全的,因此处理速度要比StringBuffers快。
<ol class="dp-j"> <li class="alt"><span><span>def sb = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> StringBuffer(</span><span class="string"><font color="#0000ff">'Hello World'</font></span><span>) </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb.toString() == </span><span class="string"><font color="#0000ff">'Hello World'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>sb.length = </span><span class="number"><font color="#c00000">5</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb.toString() == </span><span class="string"><font color="#0000ff">'Hello'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb.substring(</span><span class="number"><font color="#c00000">0</font></span><span>,</span><span class="number"><font color="#c00000">2</font></span><span>) == </span><span class="string"><font color="#0000ff">'He'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="comment"><font color="#008200">//Use </font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="comment"><font color="#008200">//Using + to append to a StringBuffer will return a String </font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>sb = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> StringBuffer(</span><span class="string"><font color="#0000ff">"Hello"</font></span><span>) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb + </span><span class="string"><font color="#0000ff">' World'</font></span><span> in java.lang.String </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb <span class="string"><font color="#0000ff">' World'</font></span><span> in java.lang.StringBuffer </span></span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb.toString() == (</span><span class="string"><font color="#0000ff">"Hello World"</font></span><span>) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb.append(</span><span class="string"><font color="#0000ff">", Groovy rocks"</font></span><span>).toString() == </span><span class="string"><font color="#0000ff">"Hello World, Groovy rocks"</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb.leftShift(</span><span class="string"><font color="#0000ff">". Doesn't?"</font></span><span>).toString() == </span><span class="string"><font color="#0000ff">"Hello World, Groovy rocks. Doesn't?"</font></span><span> </span> </li> </ol>
您也可以对StringBuffer或StringBuilder使用下标,如:
<ol class="dp-j"> <li class="alt"><span><span>sb = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> StringBuffer(</span><span class="string"><font color="#0000ff">"abcdefg"</font></span><span>) </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb[</span><span class="number"><font color="#c00000">0</font></span><span>] == </span><span class="string"><font color="#0000ff">'a'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb[</span><span class="number"><font color="#c00000">2</font></span><span>..</span><span class="number"><font color="#c00000">4</font></span><span>] == </span><span class="string"><font color="#0000ff">'cde'</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb[</span><span class="number"><font color="#c00000">0</font></span><span>].</span><span class="keyword"><strong><font color="#006699">class</font></strong></span><span> == String </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb[-</span><span class="number"><font color="#c00000">6</font></span><span>..-</span><span class="number"><font color="#c00000">4</font></span><span>] == </span><span class="string"><font color="#0000ff">"bcd"</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>sb[</span><span class="number"><font color="#c00000">0</font></span><span>..</span><span class="number"><font color="#c00000">2</font></span><span>] = </span><span class="string"><font color="#0000ff">"xyz"</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb.toString() == </span><span class="string"><font color="#0000ff">"xyzdefg"</font></span><span> </span> </li> </ol>
操作StringBuffers:
<ol class="dp-j"> <li class="alt"><span><span>sb = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> StringBuffer(</span><span class="string"><font color="#0000ff">"StringBuffers are mutable"</font></span><span>) </span></span></li> <li class=""><span> </span></li> <li class="alt"> <span>sb.delete(sb.indexOf(</span><span class="string"><font color="#0000ff">" are mutable"</font></span><span>),sb.size()) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb.toString() == </span><span class="string"><font color="#0000ff">"StringBuffers"</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>sb.insert(sb.size(), </span><span class="string"><font color="#0000ff">" are mutable"</font></span><span>) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb.toString() == </span><span class="string"><font color="#0000ff">"StringBuffers are mutable"</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>sb.replace(sb.indexOf(</span><span class="string"><font color="#0000ff">"StringBuffers"</font></span><span>), </span><span class="string"><font color="#0000ff">"StringBuffers"</font></span><span>.size(), </span><span class="string"><font color="#0000ff">"StringBuilders"</font></span><span>) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> sb.toString() == </span><span class="string"><font color="#0000ff">"StringBuilders are mutable"</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>def string = </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> String(sb) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span>def string2 = string.replaceAll(</span><span class="string"><font color="#0000ff">"StringBuilders"</font></span><span>, </span><span class="string"><font color="#0000ff">"StringBuffers"</font></span><span>) </span> </li> <li class=""><span> </span></li> <li class="alt"> <span></span><span class="keyword"><strong><font color="#006699">assert</font></strong></span><span> string2 == </span><span class="string"><font color="#0000ff">"StringBuffers are mutable"</font></span><span> </span> </li> <li class=""><span> </span></li> <li class="alt"><span> </span></li> </ol>
正如您看到的,GDK在java.lang.String,java.lang.StringBuffer和java.lang.StringBuilder基础上添加了很多实用的方法。前面的例子只讨论部分方法,因此推荐您查看String的API文档,http://groovy.codehaus.org/groovy-jdk/java/lang/String.html;StringBuffer, http://groovy.codehaus.org/groovy-jdk/java/lang/StringBuffer.html; StringBuilder, http://groovy.codehaus.org/groovy-jdk/java/lang/StringBuilder.html。
在Groovy中使用字符串时,需要注意以下几点:
与Java一样,Groovy中的字符串也是不可变的(即只读的)。欲改变字符串值需使用StringBuffer或StringBuilder。
Groovy中,如果字符串如果由单引号括起来,或者由双引号或三重引号括起来但没有非转义的美元符号($),那么该字符串是java.lang.String的一个实例。您可使用JDK中任意字符串方法。另外,GDK动态注入额外的实用方法到String类。
由于Groovy会自动转型为String类,您可以在任何时候使用GStrings。
字符串可被单引号,双引号或者三重引号括起来。其中,三重引号可直接跨越数行并且输出中仍包含回车符、制表符和空格符。
Groovy使用size方法来查询一个字符串,StringBuffer或StringBuilder的长度,与查询一个集合的长度相统一。
某些字符串方法接受闭包,它明确说明需要满足的条件,例如,find,findAll,findIndexOf,every和any。这些方法都是由GDK动态注入到java.lang.Object中的。
您可以使用collect,each或者eachWithIndex方法遍历字符串,而且这些方法也属于java.lang.Object的一部分。