This is probably the most used Data Type in groovy and not only.
Simply put String is a text. Any text actually is a String.
Also String is not just a simple Data Type, it is actually a class.
What that means is that it can be manipulate using specific methods that are defined in this class.
To be noted also that every string created is actually an Object of the Class String.
….Few words on classes and Objects
I have mention to new words above Class and Object. What do this mean in programming.
Well this are the building blocks of Object Oriented Programming.
Let me elaborate on this. A Class is a blueprint base on which other Objects are built in your program.
You can think of a Class like the plans of a house.
One just not starts building a house without having a blueprint. So you will know in which order and what the dimensions and shapes of the house should be.
And the Object that gets build using this blueprint is the actual House.
You of course can use that blueprint to build 100 more houses.
Just like this a Class in programming behaves and base on that particular class you can built(instantiate) objects.
Now in Oracle sales Cloud, we don’t actually have to worry about Classes and Objects, as the system lets us make very easy all the Objects that we need, without us having to define the class and then instantiate the object base on that.
….Back to String
As I was saying Strings are any text and you can define them like so:
def myString = “This is a String”
OR
String myString = “This is my String”
In the first case the system automatically determines that the variable myString is of data type String and in the second case you specifically specify that myString is of data type String.
You can always spot a string as it is in between double quotes ” “ or single quotes ‘ ‘
As String is a class you have at your disposal several methods and also you can do some operations with Strings.
1 – Concatenation
– it is done using + sigh
def x = "This is " + "a " + "String";
println(x);
Output:
This is a String
def x = "This ";
def y = "is " ;
def z = "a string" ;
println(x+y+z)
Output:
This is a String
2 – Escape Sequences
– this are special characters, which when used with strings have a specific function, for example one of the most used escape sequences is new line:
\n -> Insert a newline in the text at this point.
def myString = "This is" +"\n" +"new line"
println(myString)
Output:
This is
new line
more escape sequences can be found here: https://docs.oracle.com/javase/tutorial/java/data/characters.html
3 – Useful methods of the String Class
a) getting the length of a string with method length()
def myString = "This is a String"
def x = myString.length()
println(x)
Output:
16
b) extracting the string between 2 delimiters
def myString = "<My Text Here>" ;
def result = myString.substring(myString.indexOf("<") + 1, myString.indexOf(">"));
println(result )
output:
My Text Here
c) removing the [ and ] when returning an array
def myArray = [1,2 ,3,4]
def string_val = substringAfter(substringBefore(myArray.toString(),']'),'[')
println(string_val )
output
1,2 ,3,4
d) replacing a string with another using replaceAll()
def myString = "<My Test Here >" ;
def newString = myString .replaceAll('<','**')
output
**My Test Here >
Note: Strings are immutable, which means you can never change the object itself. Once you create a string it can never be changed.
As you saw above each time I changed something in a string or extracted a sub-string, I created a brand new string object with the respective change.