🔥 🔥Practical Open Source is coming 🔥🔥 Propose an article about doing business with Open Source!

Beyond basics: Advanced strings with Apache Groovy

Mastering basic string manipulation is crucial in any programming language, and Groovy takes things to the next level with its clean syntax and powerful features.

In my previous article, I reintroduced the Java, and Groovy String class. This time around, I’m going to look at Groovy’s syntactic report for operations on strings. (If you haven’t installed Groovy yet, please read the intro to this series.)

You already saw the use of triple quotes or triple apostrophes to delineate a multiline string block. You also saw the use of indexing with ranges instead of the Java .substring() method.

The Groovy as operator is a nice way to facilitate conversions and conversions are often from String to some type of numeric value:

1 int fortyTwo = "42" as int
2 println "fortyTwo = $fortyTwo; class = ${fortyTwo.class}"

This when run yields:

 $ groovy Groovy10a.groovy
 fortyTwo = 42; class = class java.lang.Integer

In Java, quotes delineate char constants, but in Groovy, quotes delineate String constants. The as keyword comes in handy when a char value is needed:

1 char charC = "C" as char
2 println "charC = $charC; class = ${charC.class}"

This yields:

 $ groovy Groovy10b.groovy
 charC = C; class = class java.lang.Character

As in Java, you can use + to concatenate strings, * to create a number of duplicates and — to remove the first occurrence of a string from another string:

1  String fooBar = "foo" + " " + "bar"
2  println "fooBar = $fooBar"
3  String fooFooBar = "foo" * 2 + "bar"
4  println "fooFooBar = $fooFooBar"
5  String fooBar2 = fooFooBar - "foo"
6  println "fooBar2 = $fooBar2"

This yields:

$ groovy Groovy10c.groovy
fooBar = foo bar
fooFooBar = foofoobar
fooBar2 = foobar

In Groovy, the + operator actually uses the plus() method defined by the class. The * operator uses the multiply() method, inherited from the CharSequence class. The - operator uses the minus() method, also inherited from the CharSequence class. This allows for overriding those methods (or defining them on new classes) to make use of these operators.

Another operator that is commonly seen in Groovy, for instance with respect to List instances, is << which is associated with the append() method. However, because the String objects are immutable, applying << or append() to a String object does not change the String object itself. Instead it returns a StringBuffer result:

1  def foo = "foo"
2  def fooBar = foo << "bar"
3  println "foo $foo fooBar $fooBar fooBar.class ${fooBar.class}"

When run this yields:

$ groovy Groovy10d.groovy
foo foo fooBar foobar fooBar.class class java.lang.StringBuffer

As in Java, the backslash character \ must be escaped for it to appear in a string. For example, a pathname in that other operating system such as:

C:\TEMP\STUFF

Must be written as:

"C:\TEMP\STUFF"

This allows the backslashes to appear in the string (and not be interpreted as escape characters.) Groovy adds the slashy string which does not require escaped backslashes:

1  String s1 = "C:\\TEMP\\STUFF"
2  String s2 = /C:\TEMP\STUFF/
3  println "s1 $s1 s2 $s2 (s1 == s2) ${s1 == s2}"
4  println "s1.class ${s1.class} s2.class ${s2.class}"

This yields:

$ groovy Groovy10e.groovy
s1 C:\TEMP\STUFF s2 C:\TEMP\STUFF (s1 == s2) true
s1.class class java.lang.String s2.class class java.lang.String

Where slashy strings really come into their own are dealing with regular expressions which are typically littered with backslashes defining special characters. The slashy string eliminates the need to escape those backslashes.

For completeness, there is also a dollar-slashy string that is used to create multiline GString instances, but I don’t find that too useful.

Conclusion

Groovy has added several operators and operator-like things to the language to make life with strings better.

Disclaimer: All published articles represent the views of the authors, they don’t represent the official positions of the Open Source Initiative, even if the authors are OSI staff members or board directors.

Author

Support us

OpenSource.net is supported by the Open Source Initiative, the non-profit organization that defines Open Source.

Trending