String in groovy tutorial

This tutorial show you feature of String in Groovy.

The String in Groovy has several forms. You can define them in single quotes as well as in double quotes, like this Groovy String has lot of other features which we will talk about in this tutorial.

Single quotes string

You can define the String in single quotes in Groovy, These string are simple string below is the example code defining string in single quotes.

package com.java.connect.groovy

class GroovyStringExample {
	static void main(def args){
		// Single quotes string
		def name = 'Mahendra'
		def lastname = 'SUDA'

		println name + ' ' + lastname
	}
}

Double quotes GStrings

The Groovy has Double quotes for String which called as GString, The GString can evaluate the variable expressions inside GString, below is the example of GString.

package com.java.connect.groovy

class GroovyStringExample {
	static void main(def args){
		def name = 'Mahendra'
		def lastname = 'SUDA'
		// Double quotes string with variables evaluations
		def text = " My full name is $name $lastname"
		println(text)
	}
}

You can do Concatenation by ‘+’ operator

package com.java.connect.groovy

class GroovyStringExample {
	static void main(def args){
		def name = 'Mahendra'
		def lastname = 'SUDA'
		// Double quotes string with variables evaluations
		def fullname = " $name $lastname "
		println(fullname)
		// You can do like this also.
		def name1 = fullname - lastname
		println(name1)
		def fullname1 = name1 + lastname
		println(fullname1)
	}
}

You can get the string by indexing as well

// You can get by indexing indexing 
assert fullname[0..7] == name
assert fullname[-4..-1] == lastname

Multiline Strings

Multiline strings can be represented by three double quotes or single quotes

Muliline in single queotes

// Multi-line strings
def sentance = '''\
This is line 1, 
This is line 2'''
def lines =sentance.split('\n')
assert lines.size() == 2

Muliline in double queotes

def address = """
$fullname
Whitefiled
Bangalore
""".trim()
def lines1 =address.split('\n')
assert lines1.size() == 3

Slashy strings literals

Slashy strings behave like GStrings but preserve backslashes. The slashy string are useful when you have to handle the special characters inside the String, this behavior is great for regular expression and directory names.

Below is the example of slashy Strings.

// for file system 
def filepath = /E:\WORK\java-site\content\groovy/
// This is plan string
def plain = '\n\r\t\b\\\f\$'
println(plain)
assert plain.size() == 7
// For special chars
def slashy= /\n\r\t\b\\\f\$/
println(slashy)
assert slashy.size() == 14

Substrings

Below is the example of substings if Groovy strings.

// substrings example
def string = 'My name is spiderman'
string = string-'name'-'is' - 'My' + ' gone'
println(string)
// Replacement
println(string.replace('gone','back'))

Processing char inside String

You can process the each chars of the String in Groovy, There are different way to do this, below is the example of some of char processing.

		// You can process characters as well
		assert 'spiderman'.toList() == [
			's',
			'p',
			'i',
			'd',
			'e',
			'r',
			'm',
			'a',
			'n'
		]
		//You also can do like this and process the each char inside string
		'spiderman' as String[]
		'spiderman'.split('')
		'spiderman'.each{}


		string = "spiderman is back"
		println(string.toList().unique().sort().join())

Above are some of example on the Groovy Strings, for more on Groovy Strings you can refer here