Questions tagged [gstring]

Groovy String class

gstring is the groovy string class which implements some special features. For example, a gstring may contain code which is executed every time the gstring is converted to a string, for instance when it is output.

http://groovy.codehaus.org/Strings+and+GString

69 questions
107
votes
3 answers

String concatenation with Groovy

What is the best (idiomatic) way to concatenate Strings in Groovy? Option 1: calculateAccountNumber(bank, branch, checkDigit, account) { bank + branch + checkDigit + account } Option 2: calculateAccountNumber(bank, branch, checkDigit, account)…
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
65
votes
3 answers

Groovy different results on using equals() and == on a GStringImpl

According to the Groovy docs, the == is just a "clever" equals() as it also takes care of avoiding NullPointerException: Java’s == is actually Groovy’s is() method, and Groovy’s == is a clever equals()! [...] But to do the usual equals()…
Anuj Arora
  • 3,017
  • 3
  • 29
  • 43
12
votes
5 answers

How to avoid saying "gstring"?

I am picking up Groovy. The language is fine. But I have a non-technical problem. One of the classes is called GString. In some context, it can be misleading. And mentioning it is not very appropriate in the office, especially when some…
Sam
  • 179
  • 4
9
votes
3 answers

Create String list in Groovy

The following code in Groovy adds GStrings to the list: List args = [ 'cmd', "-Dopt=${value}" ] When I create a ProcessBuilder with this list, I get a ClassCastException. What's a groovy way to coerce the list elements to the correct type?
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
9
votes
3 answers

Avoid new break line in groovy template

I have YAML file with my configuration name applications.yaml, this data will be my bindings: applications: - name: service1 port: 8080 path: /servier1 - name: service2 port: 8081 path: /service2 Then I have a template file…
Robert
  • 10,403
  • 14
  • 67
  • 117
8
votes
2 answers

Why Map does not work for GString in Groovy?

With the following snippet I cannot retrieve gString from a map: def contents = "contents" def gString = "$contents" def map = [(gString): true] assert map.size() == 1 // Passes assert gString.hashCode() == map.keySet().first().hashCode() //…
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
6
votes
1 answer

Groovy: Nested evaluation of variables inside ${}

I there a way to do nested evaluation of "$-Strings" in Groovy like, e.g. def obj = {["name":"Whatever", "street":"ABC-Street", "zip":"22222"]} def fieldNames = ["name", "street", "zip"] fieldNames.each{ fieldname -> def result = "…
user462982
  • 1,635
  • 1
  • 16
  • 26
5
votes
1 answer

Groovy - GString being used as key versus String as key, subscript notation versus put method

In the groovy documentation, it mentions that using a GString for a key is bad: def key = 'some key' def map = [:] def gstringKey = "${key.toUpperCase()}" map.put(gstringKey,'value') assert map.get('SOME KEY') == null However, simply changing the…
solstice333
  • 3,399
  • 1
  • 31
  • 28
4
votes
5 answers

Unsafe use of user-supplied GString:s in Groovy/Grails

The GString concept in Groovy is pretty powerful (see http://groovy.codehaus.org/Strings+and+GString). GStrings let you do things like: world = "World" println "Hello ${world}" # Output: Hello World println "1+2 = ${1+2}" # Output: 1+2 = 3 println…
knorv
  • 49,059
  • 74
  • 210
  • 294
4
votes
2 answers

Escape dot from GString

I would like to learn how to escape dot in GString so groovy (1.8) does not treat it as a part of an variable inside sql.execute. I have the following code: Map dbSettings = [schemaName:"testSchema"]; String myDbPrefix =…
Skarab
  • 6,981
  • 13
  • 48
  • 86
4
votes
2 answers

When a GString will change its toString representation

I am reading the Groovy closure documentation in https://groovy-lang.org/closures.html#this. Having a question regarding with GString behavior. Closures in GStrings The document mentioned the following: Take the following code: def x = 1 def gs…
Huibin Zhang
  • 1,072
  • 1
  • 15
  • 30
4
votes
1 answer

How to avoid evaluating an GString

I'm working on extending a legacy script system using groovy. The source scripts are "java-like", so it mostly parses as a groovy script with a little pre-processing. I'm using invokeMethod() and missingMethod() to pass-through the legacy code,…
Jo-Herman Haugholt
  • 462
  • 1
  • 5
  • 15
4
votes
1 answer

Fix UnnecessaryGString violations in IntelliJ IDEA

When I analyzed my project using CodeNarc I get huge number of UnnecessaryGString violations. I can fix them one by one by pressing ALT+ENTER and choosing Convert to String option. This is very tedious process though. Is there any autofix in…
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
4
votes
2 answers

In Grails and Groovy, do Strings (in single quotes) outperform GStrings?

Since in Grails and Groovy, single-quoted Strings are different class from GStrings (double quoted Strings allowing ${variable} value injection), is it more efficient to use single quotes, except when ${variables} are being used? I am guessing that…
TriumphST
  • 1,194
  • 1
  • 10
  • 17
4
votes
2 answers

Why doesn't .collect() work in the following GString?

This works as expected in a GSP-page: ${Foo.findAllByBar(bar)} But when adding a collect statement the code breaks .. ${Foo.findAllByBar(bar).collect { it.name }} with Error 500: Could not parse script [...gsp]: startup failed, …
knorv
  • 49,059
  • 74
  • 210
  • 294
1
2 3 4 5