Skip to content

Commit c957cea

Browse files
kacperkozajzheaux
authored andcommitted
Types of string in groovy (eugenp#6287)
1 parent e2f3d60 commit c957cea

8 files changed

Lines changed: 268 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package groovy.com.baeldung.stringtypes
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class CharacterInGroovy {
7+
8+
@Test
9+
void 'character'() {
10+
char a = 'A' as char
11+
char b = 'B' as char
12+
char c = (char) 'C'
13+
14+
Assert.assertTrue(a instanceof Character)
15+
Assert.assertTrue(b instanceof Character)
16+
Assert.assertTrue(c instanceof Character)
17+
}
18+
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package groovy.com.baeldung.stringtypes
2+
3+
import org.junit.Test
4+
5+
class DollarSlashyString {
6+
7+
@Test
8+
void 'dollar slashy string'() {
9+
def name = "John"
10+
11+
def dollarSlashy = $/
12+
Hello $name!,
13+
14+
I can show you $ sign or escaped dollar sign: $$
15+
Both slashes works: \ or /, but we can still escape it: $/
16+
17+
We have to escape opening and closing delimiter:
18+
- $$$/
19+
- $/$$
20+
/$
21+
22+
print(dollarSlashy)
23+
}
24+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package groovy.com.baeldung.stringtypes
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class DoubleQuotedString {
7+
8+
@Test
9+
void 'escape double quoted string'() {
10+
def example = "Hello \"world\"!"
11+
12+
println(example)
13+
}
14+
15+
@Test
16+
void 'String ang GString'() {
17+
def string = "example"
18+
def stringWithExpression = "example${2}"
19+
20+
Assert.assertTrue(string instanceof String)
21+
Assert.assertTrue(stringWithExpression instanceof GString)
22+
Assert.assertTrue(stringWithExpression.toString() instanceof String)
23+
}
24+
25+
@Test
26+
void 'placeholder with variable'() {
27+
def name = "John"
28+
def helloName = "Hello $name!".toString()
29+
30+
Assert.assertEquals("Hello John!", helloName)
31+
}
32+
33+
@Test
34+
void 'placeholder with expression'() {
35+
def result = "result is ${2 * 2}".toString()
36+
37+
Assert.assertEquals("result is 4", result)
38+
}
39+
40+
@Test
41+
void 'placeholder with dotted access'() {
42+
def person = [name: 'John']
43+
44+
def myNameIs = "I'm $person.name, and you?".toString()
45+
46+
Assert.assertEquals("I'm John, and you?", myNameIs)
47+
}
48+
49+
@Test
50+
void 'placeholder with method call'() {
51+
def name = 'John'
52+
53+
def result = "Uppercase name: ${name.toUpperCase()}".toString()
54+
55+
Assert.assertEquals("Uppercase name: JOHN", result)
56+
}
57+
58+
59+
@Test
60+
void 'GString and String hashcode'() {
61+
def string = "2+2 is 4"
62+
def gstring = "2+2 is ${4}"
63+
64+
Assert.assertTrue(string.hashCode() != gstring.hashCode())
65+
}
66+
67+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package groovy.com.baeldung.stringtypes
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class SingleQuotedString {
7+
8+
@Test
9+
void 'single quoted string'() {
10+
def example = 'Hello world'
11+
12+
Assert.assertEquals('Hello world!', 'Hello' + ' world!')
13+
}
14+
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package groovy.com.baeldung.stringtypes
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class SlashyString {
7+
8+
@Test
9+
void 'slashy string'() {
10+
def pattern = /.*foobar.*\/hello.*/
11+
12+
Assert.assertTrue("I'm matching foobar /hello regexp pattern".matches(pattern))
13+
}
14+
15+
void 'wont compile'() {
16+
// if ('' == //) {
17+
// println("I can't compile")
18+
// }
19+
}
20+
21+
@Test
22+
void 'interpolate and multiline'() {
23+
def name = 'John'
24+
25+
def example = /
26+
Hello $name
27+
second line
28+
/
29+
}
30+
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package groovy.com.baeldung.stringtypes
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class Strings {
7+
8+
@Test
9+
void 'string interpolation '() {
10+
def name = "Kacper"
11+
12+
def result = "Hello ${name}!"
13+
14+
Assert.assertEquals("Hello Kacper!", result.toString())
15+
}
16+
17+
@Test
18+
void 'string concatenation'() {
19+
def first = "first"
20+
def second = "second"
21+
22+
def concatenation = first + second
23+
24+
Assert.assertEquals("firstsecond", concatenation)
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package groovy.com.baeldung.stringtypes
2+
3+
import org.junit.Test
4+
5+
class TripleDoubleQuotedString {
6+
7+
@Test
8+
void 'triple-quoted strings with interpolation'() {
9+
def name = "John"
10+
11+
def multiLine = """
12+
I'm $name.
13+
"This is quotation"
14+
"""
15+
16+
println(multiLine)
17+
}
18+
19+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package groovy.com.baeldung.stringtypes
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class TripleSingleQuotedString {
7+
8+
def 'formatted json'() {
9+
def jsonContent = '''
10+
{
11+
"name": "John",
12+
"age": 20,
13+
"birthDate": null
14+
}
15+
'''
16+
}
17+
18+
def 'triple single quoted'() {
19+
def triple = '''im triple single quoted string'''
20+
}
21+
22+
@Test
23+
void 'triple single quoted with multiline string'() {
24+
def triple = '''
25+
firstline
26+
secondline
27+
'''
28+
29+
Assert.assertTrue(triple.startsWith("\n"))
30+
}
31+
32+
@Test
33+
void 'triple single quoted with multiline string with stripIndent() and removing newline characters'() {
34+
def triple = '''\
35+
firstline
36+
secondline'''.stripIndent()
37+
38+
Assert.assertEquals("firstline\nsecondline", triple)
39+
}
40+
41+
@Test
42+
void 'triple single quoted with multiline string with last line with only whitespaces'() {
43+
def triple = '''\
44+
firstline
45+
secondline\
46+
'''.stripIndent()
47+
48+
println(triple)
49+
}
50+
51+
@Test
52+
void 'triple single quoted with multiline string with stripMargin(Character) and removing newline characters'() {
53+
def triple = '''\
54+
|firstline
55+
|secondline'''.stripMargin()
56+
57+
println(triple)
58+
}
59+
60+
@Test
61+
void 'striple single quoted with special characters'() {
62+
def specialCharacters = '''hello \'John\'. This is backslash - \\. \nSecond line starts here'''
63+
64+
println(specialCharacters)
65+
}
66+
67+
}

0 commit comments

Comments
 (0)