In Groovy, string manipulation can be performed using various built-in methods and operators. Some common string manipulation techniques in Groovy include concatenation using the '+' operator, converting a string to uppercase or lowercase using the toUpperCase()
and toLowerCase()
methods respectively, replacing substrings using the replace()
or replaceAll()
methods, splitting a string into an array using the split()
method, trimming whitespace using the trim()
method, and extracting substrings using the substring()
method. Additionally, Groovy provides regular expression support for more advanced string manipulation tasks.
How to convert a list of strings to a single string in Groovy?
In Groovy, you can easily convert a list of strings to a single string using the join()
method. Here's an example:
1 2 3 |
def list = ["Hello", "World", "!"] def result = list.join(" ") println result |
This will output:
1
|
Hello World !
|
In the join()
method, you specify the separator that you want to use to concatenate the strings in the list. In the example above, we used a space (" ") as the separator.
What is the findAll method for string manipulation in Groovy?
The findAll method in Groovy is used to find all occurrences of a specified regular expression pattern in a string and return them as a collection. This method is commonly used for string manipulation and pattern matching tasks. It can be called on a String object and takes a regular expression pattern as an argument. For example:
1 2 3 4 |
def text = "The quick brown fox jumps over the lazy dog" def matches = text.findAll(/(\w{4})/) assert matches == ['quick', 'brown', 'jumps', 'over', 'lazy'] |
In this example, the findAll method is used to find all four-letter words in the given text string and return them as a collection.
How to check if a string ends with a specific suffix in Groovy?
You can use the endsWith()
method in Groovy to check if a string ends with a specific suffix. Here is an example:
1 2 3 4 5 6 7 8 |
def str = "Hello, World!" def suffix = "World!" if (str.endsWith(suffix)) { println "The string ends with the suffix" } else { println "The string does not end with the suffix" } |
This code will output "The string ends with the suffix" because the string "Hello, World!" ends with the suffix "World!".
How to check if a string is empty in Groovy?
In Groovy, you can check if a string is empty using the following methods:
- Using the isEmpty() method:
1 2 3 4 5 6 |
def str = "" if (str.isEmpty()) { println "String is empty" } else { println "String is not empty" } |
- Using the blank property:
1 2 3 4 5 6 |
def str = "" if (str.blank) { println "String is empty" } else { println "String is not empty" } |
Both methods will return true
if the string is empty and false
if the string is not empty.
How to encode a string to Base64 in Groovy?
You can encode a string to Base64 in Groovy using the built-in DatatypeConverter
class. Here's an example code snippet to encode a string to Base64 in Groovy:
1 2 3 4 5 6 |
import javax.xml.bind.DatatypeConverter String originalString = "Hello, World!" String encodedString = DatatypeConverter.printBase64Binary(originalString.bytes) println "Encoded string: $encodedString" |
In this code snippet, we first import the DatatypeConverter
class from javax.xml.bind
package. Then we define the original string that we want to encode. Next, we use the DatatypeConverter.printBase64Binary()
method to encode the original string to Base64. Finally, we print the encoded string.
You can run this code snippet in a Groovy console or script to encode a string to Base64.
What is the difference between single quotes and double quotes for strings in Groovy?
In Groovy, both single quotes (' ') and double quotes (" ") can be used to define strings. However, there are some differences between the two:
- Single quotes are used to define literal strings, where the characters within the quotes are treated exactly as they appear. Example: def singleQuotedString = 'Hello World' In this case, the string "Hello World" will be stored exactly as it is written in the single quotes.
- Double quotes are used to define interpolated strings, where variables and expressions within the quotes are evaluated and replaced with their values. Example: def name = 'Alice' def doubleQuotedString = "Hello ${name}" In this case, the variable "name" will be replaced with its value 'Alice' in the string "Hello ${name}", resulting in "Hello Alice".
Overall, single quotes are used for literal strings and double quotes are used for interpolated strings in Groovy.