Assigning ".Contains" to a variable in a loop and counting matching occurances

So I have a foreach loop which checks if a string of text contains any of a set of strings specified in a file. It works as intended but what I want is to assign that specific string from file which met the condition and then count occurrences. :roll_eyes:

So I want this:

string=“that house is very big”
array={house; cat; big}
occurances=0

foreach word in array {
if ((string.contains(word)){
containing_word = word
occurances++
}
}

1 Like

Hi
welcome to uipath community
hope these steps would help you resolve this
–hope we have the array variable ready named arr_strings
–now use a for each loop and pass the above variable as input and change the type argument as string in the property panel
–inside the loop use a IF condition like this
strinvariable.ToString.Contains(item)
if true it will go to THEN part where we can use a regex expression like this with a ASSIGN activity
int_count = System.Text.RegularExpressions.Regex.Matches(strinvariable.tostring,“(”+item.ToString+“)”).Count.ToString

and use a WRITELINE activity and mention like this
item.ToString+" occurence - "+int_count.ToString

Cheers @justDom

2 Likes

@justDom
Just please Help in clearification, based in your example ist the expected Output

2 as House and big are Matching
Or
House 1
Cat 0
Big 1

Thanks for the input, I’ve actually thought about doing it another way myself just now.
The thing is I don’t really care about containing_word value if there is more than 1 occurrence. So if occurrences is not 1 then we don’t even need the actual value.
But nevertheless for the sake of being clear if we needed both values in my example I would need this output:

occurances=2
containing_word=big (the last one assigned)

1 Like

Yah
but anyhow we can have the count of each element in the array within string so that we can know which element is used and how much its used
@justDom

System.Text.RegularExpressions.Regex.Matches(strinvariable.tostring,“(”+item.ToString+“)”).Count.ToString

Does not work properly for me. It outputs something crazy like “568” when there was only one occurrence. Interestingly sometimes it does work properly, but in rare cases. String of text is a pretty straight forward piece of info - nothing special about it.

Any other ideas or ways to output the actual number? I’ve tried debugging the above way but it did lead me nowhere.

@Palaniyappan

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