Splitting text that has a comma in elements

CUSTOMER CHANNEL BRANCHES
Customer A Channel B Branch A (Stores Company, Super Prices, United Company)

I have A table with 3 columns and multiple elements in each row , the third column has different branches

I was to divide them so they can give me the third column this way
Branch A-Stores Company
Branch A super prices
Branch A united company

so i can add them later on to my list that has
Customer A Channel B Branch A stores company
Customer A Channel B Branch A super prices

this is the end result I want
and i need to do that for every row in my table
but first let me find a way to divide them as mentioned then i can sort them
Divide by , or () or both in this case idk how

If you can get the String, then you can manipulate it with String modifiers.
https://www.tutorialspoint.com/groovy/groovy_strings.htm

The one that I was thinking of is “split()”. The “split()” modifier expects a RegEx parameter, but basically you put in the character(s) you want to break your String apart with and it creates a list of the broken parts. So to break your String on the parenthesis, you need to “escape” the parenthesis as it is used in RegEx (e.g. ("\\(")). Whereas, we wouldn’t need to do anything with a comma as it is not used in RegEx (e.g. (",") ), then we can just break up the list of comma separated items. Lastly, you would have to remove the last parenthesis, but I would probably just use “substring()” for that.
And, we can use the “trim()” modifier to remove any leading or trailing blank spaces from the list of Strings.

Maybe like:

I’m not sure if this will work, but it is a example of what I was thinking of.

def customer = "Customer A Channel B Branch A (Stores Company, Super Prices, United Company)"

def bStores = customer.split("\\(")[1]  // this get the second part which is the list of stores

List names = bStores.split(",")     // this gets the list of comma separated items

names[names.size() - 1] = names.last().toString().substring(0, names.last().toString().length() - 1)   // remove the last parenthesis

for (int cnt = 0; cnt < names.size(); cnt++) {
	println ("the names are ${names.get(cnt).toString().trim()}")
}

Edit: To remove uncertainty, I found it was easiest to add the “toString()” because the Strings would become Objects (a String is an Object but an Object is not always a String), such as “names.get(cnt)” and “names.last()”.

I guess this will work but I am Having a problem because I need to have the initial text in a list so what is happening is that i am getting them split but i also need them in this form
Customer A Channel B Branch A stores company
Customer A Channel B Branch A super prices

not only the text split
and i need to do the same for every row in the table

Okay. As I mentioned above, once you have the String, you can “play” with it. That’s the beauty of using the String modifiers. You can have so much fun.

Do you see this line in my “thought bubble” above:

def bStores = customer.split("\\(")[1]  // this gets the **second** part which is the list of stores

So, how to get the front half is:

def channels =  customer.split("\\(")[0]  // this gets the **first** part

The “split()” creates a List of the parts of the String that it “breaks” apart. In this case, the first part (Lists start at zero) is from the start up to the parenthesis and the second part is from the parenthesis to the end.

Do you know how to make a Keyword in KS? You can use a Keyword to reduce the clutter in your code and at the same time, you can repeat the same process on each of the Strings of your List (over and over and over again) and return what you want. (Maybe two Keywords. The first Keyword would return the first part of the String. Then another Keyword to return the List of stores. Maybe even a third Keyword to “concat()” the results of the first Keyword and second Keyword together to get your final results.) You don’t need to use Keywords, but it would reduce the “clutter” in your code (maybe go with Keywords after you get something working).

Maybe like:

I will let you do with the full name as you want, but I just printed them out, but you could still create a Keyword to do it should you want.

List<String> yourList = ...

for (int i = 0; i < yourList.size(); i++) {
	//def customer = "Customer A Channel B Branch A (Stores Company, Super Prices, United Company)"
	def customer = yourList.get(i);
	
	def channels = CustomKeywords.'ais.Reporting.getFrontPhrase'(customer)
	
	List names = CustomKeywords.'ais.Reporting.parseCustomers'(customer)
	
	for (int cnt = 0; cnt < names.size(); cnt++) {
		println ("the stores are ${channels} ${names.get(cnt)}")
	}
}

And the Keywords I made:

	@Keyword
	def String getFrontPhrase(String customer){
		def channels = customer.split("\\(")[0] ; // this get the first part which is the phrase
        channels = channels.trim();
		return channels;
	}


	@Keyword
	def List parseCustomers(String customer) {
		def bStores = customer.split("\\(")[1] ; // this get the second part which is the list of stores
		
		List names = bStores.split(",") ;    // this gets the list of comma separated items
		
		names[names.size() - 1] = names.last().toString().replace(")", "") ;  // remove the last parenthesis

		for (int cnt = 0; cnt < names.size(); cnt++) {
		   names[cnt] = names.get(cnt).toString().trim();
	    }
		return names;
	}
1 Like