Search a string in multiple lists

Hi All,

How do we search for a string in multiple string using C# in UiPath ?

Hi @Abc_Xyz1 ,

Could you provide us with an example input and output of what you are expecting to be performed ?

We don’t have yet the clarity of whether a list/Array of Strings is to be used.

HI @Abc_Xyz1

Few sample input and expected out would be better , can you please share that as well?

List1=“ab”,“bd”,“cd”
List2=“d”,“e”,“f”
List3=“g”,“h”,“i”

I have a array str1=a,b,c which I would be looping through
The expectation is that it should be able to search in all 3 lists and find strings containing a,b,c

I hope this clarifies the requirement.

List1=“ab”,“bd”,“cd”
List2=“d”,“e”,“f”
List3=“g”,“h”,“i”

I have a array str1=a,b,c which I would be looping through
The expectation is that it should be able to search in all 3 lists and find strings containing a,b,c

I hope this clarifies the requirement.

@Abc_Xyz1 ,

I believe Inside the loop, the Check will be performed with the lists separately, if that is the case, then you can check the below method :

list1.Where(Function(x)Str1.Any(Function(y)x.Contains(y))).ToArray

Similarly we need to perform it using the other list variables.
image

Let us know if this is not what you require.

kindly note porting from VB to C# we do

Operator(function (x) x.....Operator(x => x.
we respect the letter cases of methods, properties …
will end a statement with ;

1 Like

This syntax doesn’t work for C#. And also is there a way to check the string in all lists at one go ?

If you don’t mind using the Invoke Code activity:

var List1 = new List<string>();
var List2 = new List<string>();
var List3 = new List<string>();
List1.AddRange(new string[] {"ab", "bd", "cd"} );
List2.AddRange(new string[] {"d", "e", "f"} );
List3.AddRange(new string[] {"g", "h", "i"} );

var arrStr = new string[] {"a", "b", "c"};
var allMatches = new List<string>();

foreach (string str in arrStr) {
	foreach (List<string> lst in new List<string>[]{List1, List2, List3}) {	
		var matches  = lst.FindAll(x => x.Contains(str));
		allMatches.AddRange(matches);
	}
}

// Use Distint() if you only want unique matches.
allMatches = allMatches.Distinct().ToList();

Console.WriteLine("All matches:");
Console.WriteLine(string.Join(Environment.NewLine, allMatches));

image

I tried in VB and it did work. can you tell instead of checking for each list separately can we not check in all lists and if there is a match?

Will this work for arrstr which is dynamic ?

Yes, it should.

VB

{List1, List2, List3}.SelectMany(Function (x) x).Where(Function (x) Str1.Any(Function (y) x.Contains(y))).toArray

C# portings as mentioned above

1 Like

Can you give an example if the arrstr is coming from Excel .How do we handle dat?

arrStr is just an array of strings (the one you call Str1). The code will work as long as it’s an array (or a list). It doesn’t matter where the data comes from.

I included the initialization code to provide a working example. Sorry if it confuses you more than it helps.

In your project the variables List1, List2, List3 and Str1 should already have been initialized with values so the Invoke Code should only contain this part of the code:

allMatches = new List<string>();

foreach (string str in Str1) {
	foreach (List<string> lst in new List<string>[]{List1, List2, List3}) {	
		var matches = lst.FindAll(x => x.Contains(str));
		allMatches.AddRange(matches);
	}
}

// Use Distint() if you only want unique matches.
allMatches = allMatches.Distinct().ToList();

And the Invoke Code arguments should look like:

Note that I have renamed arrStr to Str1 which hopefully will make things easier to understand.

If you find the arguments import cumbersome, then it’s probably better to use ppr’s approach.

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