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

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
B
Blog
博客园 - Franky
WordPress大学
WordPress大学
小众软件
小众软件
腾讯CDC
博客园 - 【当耐特】
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
GbyAI
GbyAI
博客园_首页
D
Darknet – Hacking Tools, Hacker News & Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google Online Security Blog
Google Online Security Blog
Stack Overflow Blog
Stack Overflow Blog
Jina AI
Jina AI
L
Lohrmann on Cybersecurity
E
Exploit-DB.com RSS Feed
C
Cybersecurity and Infrastructure Security Agency CISA
H
Heimdal Security Blog
N
Netflix TechBlog - Medium
Know Your Adversary
Know Your Adversary
大猫的无限游戏
大猫的无限游戏
V
Vulnerabilities – Threatpost
C
CERT Recently Published Vulnerability Notes
雷峰网
雷峰网
K
Kaspersky official blog
B
Blog RSS Feed
Recent Announcements
Recent Announcements
T
The Exploit Database - CXSecurity.com
The Cloudflare Blog
Cloudbric
Cloudbric
Y
Y Combinator Blog
D
Docker
S
Secure Thoughts
Simon Willison's Weblog
Simon Willison's Weblog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyberwarzone
Cyberwarzone
S
Securelist
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
W
WeLiveSecurity
MongoDB | Blog
MongoDB | Blog
P
Privacy & Cybersecurity Law Blog
The Register - Security
The Register - Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
AWS News Blog
AWS News Blog

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 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