Operadores y Cadenas

Algunos operadores aritméticos han sido extendidos para trabajar con cadenas:

generaciondecodigos@nereida:~/src/groovy/strings$ groovysh
Groovy Shell (1.6.5, JVM: 1.6.0_0)
Type 'help' or '\h' for help.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
groovy:000> a = "Hello"
===> Hello
groovy:000> b = a - 'o'
===> Hell
groovy:000> b = a*2
===> HelloHello
groovy:000> b = a + ' World!'
===> Hello World!

Veamos algunos ejemplos tomados de http://pleac.sourceforge.net/pleac_groovy/strings.html

//----------------------------------------------------------------------------------
// accessing substrings
string = 'hippopotamus'
start = 5; end = 7; endplus1 = 8
assert string.substring(start, endplus1) == 'pot'
assert string[start..end] == 'pot'

assert string.substring(start) == 'potamus'
assert string[start..-1] == 'potamus'

// String is immutable but new strings can be created in various ways
assert string - 'hippo' - 'mus' + 'to' == 'potato'
assert string.replace('ppopotam','bisc') == 'hibiscus'
assert string.substring(0, 2) + 'bisc' + string[-2..-1] == 'hibiscus'
// StringBuffer is mutable
sb = new StringBuffer(string)
sb[2..-3] = 'bisc'
assert sb.toString() == 'hibiscus'
//----------------------------------------------------------------------------------
El método split permite obtener una colección a partir de una cadena:
generaciondecodigos@nereida:~/src/groovy/strings$ cat -n split.groovy
     1  aaa = '"bread","apple","egg"'
     2  items = aaa.split(',')
     3  assert items[1] == '"apple"'
     4  items.each{ println "item: $it" }
generaciondecodigos@nereida:~/src/groovy/strings$ groovy split.groovy
item: "bread"
item: "apple"
item: "egg"

La asignación de cadenas producen una copia de la cadena como muestra el siguiente ejemplo:

generaciondecodigos@nereida:~/src/groovy/strings$ cat -n assignments.groovy
     1  println "-------------maps----------"
     2  st = ["status":"test"]
     3  sn = st
     4  println "Before sn=$sn st=$st"
     5  st.status = "tset"
     6  println "After sn=$sn st=$st"
     7
     8
     9  println "-------------GStrings------------"
    10  st = "test"
    11  sn = st
    12  println "Before sn=$sn st=$st"
    13  st = "tset"
    14  println "After sn=$sn st=$st"
    15
    16  println "-------------Strings------------"
    17  st = 'test'
    18  sn = st
    19  println "Before sn=$sn st=$st"
    20  st = 'tset'
    21  println "After sn=$sn st=$st"
    22
generaciondecodigos@nereida:~/src/groovy/strings$ groovy assignments.groovy
-------------maps----------
Before sn=[status:test] st=[status:test]
After sn=[status:tset] st=[status:tset]
-------------GStrings------------
Before sn=test st=test
After sn=test st=tset
-------------Strings------------
Before sn=test st=test
After sn=test st=tset

Here, sn and st point at the very same map object in memory in the first example, while in the second snippet, at the end, st points at a different place in memory where there's the new immutable string.

Casiano Rodríguez León
2010-04-30