Beginning soapUI Scripting 2: Strings

You can download a "shell" project to import into soapUI and use for scripting here.

Like my first post on scripting, the material in this post is not specific to soapUI, but strings are used so extensively I thought they were worth a brief explanation for beginners who may not be familiar with them.  Put simply, strings are text data, or a sequence of characters; "Hello World!" is an example of a string. The syntax to create a string variable is similar to the syntax we saw in my first post when creating an integer variable:

def myVar = 3
def myString = "Hello World!"


These lines assign the value 3 to the variable myVar and the string "Hello World!" to the variable myString.  Note the use of quotes, one key difference with strings-- those demarcate our text, but are not included with the actual data.  As with the integer variable myVar, once the myString variable has been created, we can reference its value in subsequent lines of our script using its name:

log.info(myVar)
log.info(myString)


This code prints out the values of our variables to the log:



As promised, the enclosing quotes used when the string was created are not included when the string is printed.

Strings can be enclosed in either single quotes or double quotes in Groovy (but not in "pure" Java-- in Java, strings are always enclosed in double quotes).  You can use a mix of both when you want to include one or the other in your string.  If you want to include a single quote as part of your string, enclose the string in double quotes, and vice versa; for example:

def stringOne = "Don't panic."
def stringTwo = '"Look, I have double quotes!"'
log.info(stringOne)
log.info(stringTwo)


This produces:



You can also perform variable substitutions in Groovy; variable names preceded by the "$" character that are enclosed in strings are replaced with their underlying values.  For example:

def myVar = 3
def myString = "The value of myVar is $myVar"
log.info(myString)


The resulting output:



One thing to remember: variable substitution only works with strings enclosed in double quotes-- in a  string enclosed in single quotes, $myVar would be interpreted as a literal (simply printed out "as is" as  "$myVar").

Finally, while strings may behave in some ways like Java's so-called primitives (the basic data types that include integers), they are in fact objects of class String.  Consequently, they have quite a few methods at their disposal that can be used to parse and alter their contents.  There are far too many methods to cover all of them here; see the Java API entry for the String object to see the full list.  Here's some code that demonstrates a few of them, however (the lines that start with "//" in the script are comments-- they're only there for informational purposes):
def stringOne = "   One, two, three    "
log.info("stringOne = '$stringOne'")
//Trim method-- removes leading and trailing whitespace from a string
def stringTrimmed = stringOne.trim()
log.info("Trimmed string = '$stringTrimmed'")
//Contains method-- checks if one string is contained in another
log.info("String 'two' in '$stringTrimmed'?")
log.info(stringOne.contains("two"))
//Length method-- returns the number of characters in the string
log.info("Length of '$stringTrimmed':")
log.info(stringTrimmed.length())
//EndsWith method-- checks if string ends with certain characters
log.info("'$stringTrimmed' ends with 'three'?")
log.info(stringTrimmed.endsWith("three"))
//StartsWith method-- checks if string starts with certain characters
log.info("'$stringTrimmed' starts with 'Two'?")
log.info(stringTrimmed.startsWith("Two"))
And here's the output:

No comments:

Post a Comment

Please be respectful of others (myself included!) when posting comments. Unfortunately, I may not be able to address (or even read) all comments immediately, and I reserve the right to remove comments periodically to keep clutter to a minimum ("clean" posts that aren't disrespectful or off-topic should stay on the site for at least 30 days to give others a chance to read them). If you're looking for a solution to a particular issue, you're free to post your question here, but you may have better luck posting your question on the main forum belonging to your tool's home site (links to these are available on the navigation bar on the right).