How to use switch on a string

datatypes

#1

Hi,

I’m trying to use the switch activity to do different activities if a variable I scraped contains specific words.

For example, if contains “hello” do an activity, and if contains “bye” another one…


How to pattern match the string
#2

@nSangui,

You need to change the type of argument of switch activity to string ,

Hope this sample example will resolve.

Test.xaml (5.4 KB)

Thank You :slight_smile:


#3

Hi, thanks for your response, I will use this issue.

So, if I understand, if we have a string we must isolate the word we want to be able to do the switch to verify the equality.


#4

Can I use string variable in Switch Case insted of constatnt string?


#5

Thanks for this sample. It was counterintuitive to me that we do not put the case values in double quotes… as we would for every other string in UIPath.


#6

thanks. i have been struggling with the switch process for hours! i used “” as per other values. removed the “” and boom it works.


#7

thank you !!!
your example helped to solve a milestone!!!:smiley::smiley:


#8

How do we use “contains” for example in the Test file it works if I type Hi or Bye… but what if I have a string that says “Hi John how are you today?” and I want to search for the keyword “Hi” within this entire string I pass… Right now the test file doesn’t handle this scenario. Thanks for the help!


#9

Hi.

Did you get a response elsewhere?
I’m facing the same issue here!


#10

@FlpVsg
In the past, what I have done is used the If() inside the condition for each word.
For example,

If( txt="path1","PATH1", If( txt="path2","PATH2", If( txt="path3","PATH3", "Default" ) ) )

Technically, you can use any word for the path and use it in the condition. EDIT: you can also use .Contains(), .StartsWith(), etc instead of =

I don’t know if this is what you are looking for, but it is one way to accomplish a Switch.


#11

That’s what a I did!
Thanks :smiley: