In Groovy, you can format strings by using the String.format()
method. This method works similarly to the standard Java String.format()
method.
You can use format specifiers like %s
for strings, %d
for integers, %f
for floating-point numbers, and so on.
For example, you can format a string like this:
1 2 3 4 |
def name = "John" def age = 30 def formattedString = String.format("My name is %s and I am %d years old.", name, age) println(formattedString) |
This will output:
1
|
My name is John and I am 30 years old.
|
You can also use GString interpolation to format strings by using ${}
inside a double-quoted string.
For example:
1 2 3 4 |
def name = "Jane" def age = 25 def formattedString = "My name is ${name} and I am ${age} years old." println(formattedString) |
This will output:
1
|
My name is Jane and I am 25 years old.
|
Overall, Groovy provides several options for formatting strings in a flexible and easy-to-use manner.
What is string interpolation in Groovy?
String interpolation in Groovy allows you to embed variables and expressions directly within double-quoted strings. This means that you can easily include the value of variables and the result of expressions within a string without having to concatenate them using the +
operator.
For example, consider the following code snippet:
1 2 3 4 5 |
def name = "Alice" def age = 30 def message = "Hello, my name is $name and I am $age years old." println message |
In this example, the variables name
and age
are embedded within the string message
using the $
symbol. When the println
statement is executed, it will output: Hello, my name is Alice and I am 30 years old.
String interpolation in Groovy makes it easier to create and manipulate strings by allowing you to directly reference variables and expressions within a string literal.
How to format strings in Groovy using printf method?
To format strings in Groovy using the printf method, you can use placeholders and format specifiers to define how each variable should be displayed. Here's an example:
1 2 3 4 5 6 |
def name = "John" def age = 30 def height = 5.9 String formattedString = "%s is %d years old and is %.2f feet tall".printf(name, age, height) println(formattedString) |
In the above example:
- %s is a placeholder for a string variable (name)
- %d is a placeholder for an integer variable (age)
- %.2f is a placeholder for a floating-point variable (height) with 2 decimal places
When you call printf
on the format string and pass in the variables, the method will replace the placeholders with the actual values and return the formatted string. The output will be:
1
|
John is 30 years old and is 5.90 feet tall
|
What is the difference between GString and String in Groovy?
In Groovy, a GString is a special type of string that allows for string interpolation, or the embedding of expressions within a string, similar to string templates in other languages. GStrings are enclosed in double quotes and allow for the use of placeholders denoted by ${}, which can contain Groovy expressions that are evaluated and inserted into the final string.
On the other hand, a regular String in Groovy is just a plain string literal enclosed in quotes, without any special interpolation features. Regular strings do not allow for the embedding of expressions and are treated as plain static text.
In summary, the main difference between GString and String in Groovy is that GString allows for interpolation and dynamic content, while String is a static, literal string.
How to use String interpolation for formatting strings in Groovy?
In Groovy, you can use string interpolation to embed expressions or variables within a string literal. Here's how you can use string interpolation in Groovy for formatting strings:
- Simple interpolation with double quotes:
1 2 3 4 |
def name = "John" def age = 30 def message = "Hello, ${name}. You are ${age} years old." println message |
- Interpolation with triple double quotes for multi-line strings:
1 2 3 4 5 6 7 |
def name = "Alice" def age = 25 def description = """ Name: ${name} Age: ${age} """ println description |
- Interpolation with GString:
1 2 3 4 5 |
def name = "Bob" def age = 35 def gString = new GStringImpl(["${name}", "${age}"], ["", " years old"]) def message = "Hello, ${gString}." println message |
String interpolation in Groovy makes it easy to incorporate variables and expressions within a string, allowing for dynamic formatting of strings.
What is the role of curly braces in String interpolation in Groovy?
Curly braces in string interpolation in Groovy are used to embed expressions or variables within a string. Groovy will evaluate the expression or variable within the curly braces and include the result in the final string. This allows for dynamic content to be included in a string without the need for concatenation.
For example:
1 2 3 4 |
def name = "John" def age = 30 println "Hello, my name is ${name} and I am ${age} years old." |
In this example, the expressions name
and age
within the curly braces will be evaluated and their values will be included in the final string. The output will be:
1
|
Hello, my name is John and I am 30 years old.
|
What is the best practice for formatting strings in Groovy?
One best practice for formatting strings in Groovy is to use GString interpolation, which allows you to include variables and expressions directly within a string by enclosing them in ${}.
For example:
1 2 3 4 5 |
def name = "Alice" def age = 30 def greeting = "Hello, ${name}! You are ${age} years old." println greeting |
This will output:
1
|
Hello, Alice! You are 30 years old.
|
Using GString interpolation makes your code cleaner and easier to read, as it avoids the need for cumbersome concatenation with the + operator.