Strings, GStrings and Numbers

Site Admin · 11 Sep 2026 · 9 views

String Types

Groovy offers two string types: regular strings (single-quoted) and GStrings (double-quoted or slash-delimited). Regular strings are simple text, while GStrings support variable interpolation.

Working with Strings

// Single-quoted - no interpolation
def simple = "Hello World"

// Double-quoted - GString with interpolation
def name = "Groovy"
def gstring = "Hello ${name}!"

// Slash-delimited - no escaping needed for special characters
def path = /C:\Users\docs\file.txt/

// Multi-line strings
def multiline = """
    This is
    a multi-line string
"""

String Methods

Groovy adds many useful methods to String:

def str = "Hello, Groovy!"
println str.toUpperCase()     // HELLO, GROOVY!
println str.reverse()        // !yvoorG ,olleH
println str.take(5)          // Hello
println str.capitalize()     // Hello, groovy!
println str.center(20, "*") // **Hello, Groovy!****

Numbers in Groovy

Groovy supports all Java numeric types and adds convenience features:

// Underscores for readability
def million = 1_000_000

// Binary and hex literals
def hex = 0xFF
def binary = 0b1010

// BigDecimal with suffix
def price = 19.99g

// BigInteger with suffix
def big = 123456789g

Key Points

  • Single-quoted strings are plain; double-quoted are GStrings with interpolation.
  • Slash-delimited strings avoid escaping backslashes.
  • Groovy adds many convenience methods to String.
  • Numeric literals support underscores for readability.
  • g suffix creates BigDecimal; G creates BigInteger.
Share this post:

Comments (0)

Please login or register to comment.