Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of StartsWith and Contains in List<string>?

Tags:

c#

list

I have a list.

It's possible members (x123, y123, z123, a123, b123, c123).//123 is example This "mylist" may contain a member that starting with x, or may not. Also this is the same for y,z,a,b,c.

If contains a member starts with x:
//Formula Contains X

If Not Contains a member starts with x:
//Formula Not Contains X

//same as all of x,y,z,a,b,c. But unlike a foreach, I must place the formulas at checking time, not after.

How can I do that?

like image 296
ithnegique Avatar asked Jun 04 '13 18:06

ithnegique


People also ask

Can we use Startswith in list python?

Python example code use str. startswith() to find it a string starts with some string in a list. In the example list of substrings has converted into a tuple using tuple(list) in order to return a boolean if str contains substrings found it.

How do you check if a list contains a string?

if (myList. Contains(myString)) string element = myList. ElementAt(myList. IndexOf(myString));

How do you check whether a list contains a specific element in C#?

List<T>. Contains(T) Method is used to check whether an element is in the List<T> or not.

How do you check if a string starts with another string in Python?

Python String startswith() method returns True if a string starts with the specified prefix (string). If not, it returns False.


1 Answers

Checks if any items start with 'x' in your list:

bool result = mylist.Any(o => o.StartsWith("x"))

Checks if no items start with 'x' your list:

bool result = !mylist.Any(o => o.StartsWith("x"));
like image 187
Fabian Bigler Avatar answered Sep 23 '22 17:09

Fabian Bigler