Parsing Strings in Katalon – Split, Substring and Readlines

Once text has been read from the page, it’s time to do something with it. In many cases, only a portion of the text is needed. For example, you may need the first part of a text string, or the last part. An example text string would be:

(1/26/2018 – DAY 19 OF 22)

The first thing is to read in the entire string into a variable:

importDate = WebUI.getText(findTestObject(‘Page_/Sales Dashboard/mtd_Sales_Dashboard_Import_Date’))

Since I want the text to the left of the hyphen, I can use that in my split command. The [0] at the end indicates I want text to the left. If I change it to [1] it would be text to the right.

parsedDate = (importDate.split(‘ – ‘)[0]) – returns (1/26/2018

parsedDate = (importDate.split(‘ – ‘)[1]) – returns DAY 19 OF 22)

From there I can manipulate the text further for my needs.

It’s also possible to break the line at the hyphen and return the resulting two strings into a List.

String[] parsedDate = importDate.split(‘ – ‘)

Since it is now a list, it can be referenced by index:

importDate = parsedDate[0] – sets importDate to the text  (1/26/2018

businessDays = parsedDate[1] – set businessDays to the text DAY 19 OF 22)

The above commands accomplish the same thing, the the first uses a single variable, the second uses a List.

The List comes in handy for something like an address. If the address is Charlotte, NC 28203, the address can be split at the comma.

String[] splitAddress=branchAddress.split(‘, ‘)

splitAddress[0] – set to Charlotte

splitAddress[1] – set to NC 28203

Instead of splitting a string at a predetermined character, you may need to split the string after a certain number of characters. For example, Branches are listed as:

Branch #123 – Charlotte, NC

Branch #140 – Huntersville, NC

Branch #005 – Rock Hill, SC

For this example, I need the Branch number. Since the first part of the string is always the same, the Substring command can be used to retrieve X number of characters. Substring starts at position 0.

branchName=tempBranchName.substring(0,11) – Returns 11 characters – Branch #005

If I only wanted the number of the branch, I would start at position 7, the space after branch and read to position 11, the end of number.

branchName=tempBranchName.substring(7,11) – Returns – #005

The .readlines() command performs parsing on multiple lines worth of text from a cell and breaks them into individual lines. An address in a textbox would be a use of .readlines()

For example, the full text would be:

1313 Mockingbird Ln

Charlotte NC, 28203

Since all of that comes in as one block of text, it needs to be parsed into lines. Once the text has been set to a variable using the GetText command, the following commands can parse through it.

branchAddress=WebUI.getText(findTestObject(‘Page_/Search Branch/Branch Profile Address’))

//Get the Branch Profile Address from the return results and parse it into multiple lines

//The address will be compared to the address from the Search Results page

branchProfileAddress = branchAddress.readLines() – Is a list consisting of the two lines of the address

branchProfileStreetAddress=branchProfileAddress[0] – Returns 1313 Mockingbird Ln

branchProfileCityState=branchProfileAddress[1] – Charlotte NC, 28203

Other articles of interest:

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments
  • How To Disable the Quicken Registration Prompt (21)
    • Susan Long: I bought my quicken disc in the beginning and it came with a registration number. I rang the helpline and they gave me the code to put in and talked me through it. It you downloaded your version then you don’t own it...
    • Peter: You might consider running your old version of Quicken on an ancient computer. This is what I have done for years. The newer versions are fraught with problems- criminal, in my opinion. One version made mathmatical errors when...
    • Roslyn Chamberlain: Can I stop the countdown in quicken 2001 says only 8 sessions left. and what will happen after?
  • What is Katalon Studio? A Distro of Selenium, Groovy and Eclipse (1)
    • Mahesh: Looking for more posts on katalon studio.your katalon stuffs are always exiting
  • Simple wildcard searches for pattern matching (2)
    • Don Pedro: For that scenario it seems .contains would be your choice. For example, variable.contains(‘amazo n.com’) to see if the url had amazon.com. In that case, www.amazon.com, forum.amazon.com, retail.amazon.com would all...
    • Jony: Hi, How can I use a wild card to assert a URL is the one I want. I just want to verify the domain ==expected but not anything after it. Tried * but not working and only works when I have full URL.
  • Create a Dynamic Object at Runtime (2)
    • Saish: How to add shadow root parent to this runtime object..
    • Jeremy Brien: I appreciate this! I saw this post on LinkedIn this morning and was able to find a use case for it! I found that defining my xpath with an iterable variable allows me to loop through and capture text from tables created...
  • Output status messages and test information by writing to the Log File Viewer in Katalon Studio (2)
    • Don Pedro: That would be things like: log.logWarning(‘The import date listed on the site is: ‘ + importDate) log.logWarning(‘The value from the site is: ‘ + salesFigure) log.logWarning(‘Filter Results for ‘ +...
    • Prashant Pednekar: Thanks for the informative article but can we also put some variable values to make it more exciting.
  • Setting up a repeatable Search Method in Katalon Studio (1)
    • RJ: Thanks a lot! This is very helpful 🙂
  • Filling forms with random numbers in Katalon Studio (3)
    • Patrick Clough: We created a handy random string generator custom keyword. The method takes in a string for the type of string you want, and an integer for the length. Looks like this: static String RANDOM_ALPHANUM =...
    • Don Pedro: randomNumber is the variable that holds the result of generating a new random number. Depending on how things work on your site, you might need to convert the number to a String before it’s recognized correctly....
    • Seth: When you are setting up randomNumber is this a new keyword or a new variable? I am trying to randomly generate unique socials and can’t seem to connect my generator to my Set Text.
  • Working with Dates and Date Formatting in Katalon Studio (5)
    • Ann: THANK YOU ! That worked perfectly !