How to use switch on a string

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…

2 Likes

@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:

12 Likes

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.

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

2 Likes

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.

27 Likes

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

9 Likes

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

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!

Hi.

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

@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.

3 Likes

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