---
title: How to use Groovy to work with Strings
description: Cycle includes steps to execute simple Groovy statements inline as well as execute complex Groovy scripts. 
---

[Skip to content](https://kb.cyclelabs.io/how-to-use-groovy-to-work-with-strings#main-content)

English - United States

Show submenu for translations

![](https://kb.cyclelabs.io/hs-fs/hubfs/main-lockup-color-Aug-11-2026-06-33-05-0386-PM.png?width=500&height=93&name=main-lockup-color-Aug-11-2026-06-33-05-0386-PM.png)

Open main navigation

Close main navigation

- English - United States
  
  Show submenu for translations
- [Get Support & Track Tickets](https://help.cyclelabs.io/)

[Get Support & Track Tickets](https://help.cyclelabs.io/?hsLang=en-us)

 How can we help you?

- There are no suggestions because the search field is empty.

1. [Cycle Knowledge Base](https://kb.cyclelabs.io/?hsLang=en-us)
2. [Tips and Tricks](https://kb.cyclelabs.io/tips-and-tricks?hsLang=en-us)

# How to use Groovy to work with Strings

## Cycle includes steps to execute simple Groovy statements inline as well as execute complex Groovy scripts.

The 2 Cycle steps that execute Groovy are: 

I execute Groovy " "   
I execute Groovy script " "

**This article assumes a conceptual familiarity with Groovy/Java including general syntax, data types, import statements, class references and method usage.**

For more information on Groovy see [Apache Groovy](http://groovy-lang.org/)

Two items of note regarding Cycle and Groovy.

- 1\. Cycle can only input and output **String** and **Number** data types with Groovy statements and scripts. 
    - Dates, arrays, lists, maps are not yet functional inputs or outputs but can be used within a Groovy script.
- 2\. Cycle outputs a variable called **$groovy\_result** which represents the final result 
    - Custom variables can be assigned and used

 Examples

The following examples use methods from Groovy's **String** class does not require an explicit import statement. As such, they can easily be used as inline Groovy statements as opposed to an external Groovy script.

 Change the case of a string:

```
Scenario:Groovy To Upper and Lower CaseGiven I assign "user" to variable "username"And I assign "PASS" to variable "password"When I execute groovy "upper = username.toUpperCase()"Then I echo $upperWhen I execute groovy "lower = password.toLowerCase()"Then I echo $lower
```

This Scenario uses the **toUpperCase()** and **toLowerCase()** methods in Groovy to convert the case of string characters. In this example the Cycle variable **username** with value **user** is passed as an object to the **toUpperCase()** method and the value **USER** is returned. Similarly the Cycle variable **password** with value **PASS** is passed as an object to the **toLowerCase()** method and the value **pass** is returned

 Get the length of a string:

```
Scenario: Groovy inline lengthGiven I assign " A " to variable "spaces"Then I execute groovy "a = spaces.length()"Then I echo $a
```

This Scenario uses the **length()** method in a Groovy statement to return the total character count of the passed in string.

 Trim leading or trailing spaces:

```
Scenario: Groovy inline trimGiven I assign " A " to variable "spaces"Then I execute groovy "a = spaces.length()"Then I echo $aThen I execute groovy "noSpaces = spaces.trim()"Then I echo $noSpacesThen I execute groovy "b = noSpaces.length()"Then I echo $bThen I verify number $a is greater than $bAnd I verify number $b is equal to 1
```

This Scenario uses the **trim()** method in a Groovy statement to remove the leading and trailing spaces in the string. It builds on the use of length() in order to validate our trim() was successful.

 Replace characters in a string:

```
Scenario:Groovy Inline ReplaceGiven I assign "CASEORD001" to variable "order"Then I assign "001" to variable "oldSuffix"Then I assign "002" to variable "newSuffix"Then I execute groovy "newOrder = order.replace(oldSuffix,newSuffix)"Then I echo $newOrder
```

This Scenario uses the **replace(target,replacement)** method to replace character values within a string. The method requires target and replacement value which can be explicitly set in the call or passed in with Cycle variables. 

 Replace all characters in a string:

```
Scenario:Groovy Inline Replace AllGiven I assign "This string should have all of its spaces removed" to variable "spaces"Then I execute groovy """noSpaces = spaces.replaceAll("\\s","")"""Then I echo $noSpaces
```

Similar to **replace (target,replacement)** is **replaceAll(target,replacement)**. True to its name the replaceAll() method will change all occurrences of the target to the replacement. In the Scenario all occurrences of a space are replaced with a null string, or more simply removed. 

 Return part of a string:

```
Scenario:Groovy Inline SubstringGiven I assign "TodayIsAGreatDay" to variable "sentence"#Provide a starting point and go to the endThen I execute groovy "full = sentence.substring(0)"Then I echo $full#Provide a starting point and an endpointThen I execute groovy "great = sentence.substring(8,13)"Then I echo $great#Get the user defined endGiven I assign 3 to variable "end"Then I execute groovy "day = sentence.substring(sentence.length() - end)"Then I echo $day
```

The **substring(start, end)** method in Groovy is used to return pieces of a string. This Scenario demonstrates 3 common ways to get parts of a string.

The first way is by only providing a start parameter. In this case the characters from the start position until the end will be returned. A start index of 0 without and end index will return the original string.

The second way is providing both the start and the end. The return will be the characters in that range. In this example the sentence string charters from 8 to 13 are returned which is the word Great.

The third way is return the last X characters from a string. In this example the number of characters to grab from the end is set in a Cycle variable. That variable is passed in and subtracted from the length **(we are using length() again!)** to calculate the desired starting point. Since that is the only index provided the return is from that point until the end of the string which in this case is the word Day.

- [Getting started](https://kb.cyclelabs.io/getting-started?hsLang=en-us)
- [Using Cycle with BYWMS](https://kb.cyclelabs.io/using-cycle-with-bywms?hsLang=en-us)
- [Cycle Appliance and CI/CD](https://kb.cyclelabs.io/cycle-appliance-and-ci-cd?hsLang=en-us)
- [Tips and Tricks](https://kb.cyclelabs.io/tips-and-tricks?hsLang=en-us)
- [Native App](https://kb.cyclelabs.io/native-app?hsLang=en-us)
- [Deprecated Steps](https://kb.cyclelabs.io/deprecated-steps?hsLang=en-us)
- [Common Issues](https://kb.cyclelabs.io/common-issues?hsLang=en-us)

[![](https://kb.cyclelabs.io/hs-fs/hubfs/main-lockup-color-Aug-11-2026-06-33-05-0386-PM.png?width=500&height=93&name=main-lockup-color-Aug-11-2026-06-33-05-0386-PM.png)](http://cyclelabs.io)

<https://www.facebook.com/> <https://www.twitter.com/> <https://www.instagram.com/> <https://podcasts.apple.com/> [mailto:email@email.com](mailto:email@email.com)

Copyright © 2026, Cycle Labs