How to add switch case with Dynamic input strings

Hi Community,

I am ne RPA dev, need help1

I have data in notepad, and I need to check if that data contains some strings, if it’s it is 1 or 2 I can use if else but there are many such
strings I need to check 1 by 1 So better to go for switch case

I can use string.contains in condition , but and in cases I can pass expected strings
but I am unable to write complete condition and how to mention those strings? in array ? or list ?I ant it to be more dynamic i should be able
to add more later

samples are

  1. I like RPA
  2. UNKNOWN FORMAT IB/K
  3. PLEASE CHECK AGAIN
  4. RETRY AFTER SOME TIME

such 7 are there, can I add these in asset or in code ?

any of above will be in notepad /string

spaces between words are not fix.

TIA.

.Contains cannot be used as a condition in switch activity as that is only giving output as True or False

then, how to handle it ?

The best way to start is to describe first your use case as there might be some other approach aside from searching partial strings with .contains from those line of strings. Are the actions to be done if any of your search string is found the same? More details would help in finding how to handle the use case

@avejr748
thanks for reply ! yes if that string found then I have to throw Business exception.

Hie @Shruthika_r if i’m not wrong and the way i’m understanding you want to use switch condition and check for multiple cases if yes i’m tagging a screenshot for handle multiple
cases. cheers and happy automation.


if(strInput=“1”,“Case1”,if(strInput=“2”,“Case2”,if(strInput=“3”,“Case3”,“Default”)))
please mark this if it feels you as a solution

thanks @singh_sumit

i need to check if input string ( notepad content with 20 25 lines) contains those substring or no ? if it contains then I need to throw Business exception. I got the result using if else and contains but no BE msgs are more 6 to7, so thought to use switch case instead if else

If that is the case, any of the found string will just result to throwing a business exception then that is achievable. Assign your search string into an array

arraySearchString = {"I like RPA","UNKNOWN FORMAT IB/K","PLEASE CHECK AGAIN","RETRY AFTER SOME TIME"}

Assign your notepad text to string for example strNotepadString, then split the lines in the notepad into array

arrayNotepadString = strNotepadString.Split(Environment.NewLine.ToArray, StringSplitOptions.RemoveEmptyEntries)

Then loop into items of your arrayNotepadString using For each, inside it another For Each looping into your search strings, something like

For each notepadstring
        For each searchstring
                     If notepadstring.tolower.contains(searchstring.tolower) then
                              Throw business exception
2 Likes

hi @avejr748
thanks this working as expected; however, if Search String has no fix spaces, e.g. if it can be I like RPA or I like RPA then, it is not recognizing

Hi @Shruthika_r If that is the issue then a little modification would handle that

For each notepadstring
        For each searchstring
                     If notepadstring.replace(" ","").tolower.contains(searchstring.replace(" ","").tolower) then
                              Throw business exception

Basically you just remove all spaces in notepadstring and searching so they should match regardless of how many spaces comes in between "I like RPA"

1 Like

Hi @Shruthika_r if that has solved your issue, kindly mark it as a solution so that it would be easier for others to find. Thanks

Yes , in a while after testing I will surely revert :slight_smile:

1 Like

@avejr748 , marked as solution.

thanks a lot !

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.