惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
月光博客
月光博客
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Visual Studio Blog
博客园 - 叶小钗
博客园 - 司徒正美
美团技术团队
博客园_首页
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News

Comments for OpenSource.net

Orchestrating AI in clinical settings with jBPM – OpenSource.net jBPM as an AI orchestration platform – Part 2 – OpenSource.net jBPM as AI Orchestration Platform – Part 1 – OpenSource.net Apache OpenServerless is the easiest way to build your cloud native AI application – OpenSource.net Loops in Bash for command-line power users – OpenSource.net Preserving Open Values in artificial intelligence – OpenSource.net TLS and networking – OpenSource.net Security and cryptography algorithms: A guide – OpenSource.net Essential Python web security – OpenSource.net Crucial lessons for Artificial intelligence – OpenSource.net
Advanced strings with Apache Groovy – OpenSource.net
Chris Hermansen · 2024-01-18 · via Comments for OpenSource.net

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.

  • Chris Hermansen