How to check if an item exists in a string List and if it exists, get its index

Thundrstorm

To get the index we can use int index = myList.FindIndex(a => a.StartsWith("string_to_search")); when we are sure the string exists in myList.

But how to check if it is present in the List and get its index if it is present? For the time being, I am using the following code.

int ii=0;
foreach (var item in myList){
    if (item == "string_to_search")
        Console.WriteLine("Found at index: " + ii);
    // index is ii
    ii++;
}

Is there any better way to do this ?

Sham

Use IndexOf method to find index of item in list. This method returns -1 in case item does not exist in the list.

var itemIndex = myList.IndexOf("string_to_search")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to check if a key exists in Json Object and get its value

How can I check if a list index exists?

How to delete an item in a list if it exists?

How to check if an environment variable exists and get its value?

How to check if an item exists in an Elixir list or tuple?

How to check if a file with a certain string in its filename exists in bash?

Check if an item exists in an observableArray

How to check whether an item exists in the dynamodb table?

Check if an item exists in the array

Check if a list element exists in a string

How can I check if an item exists in a inner list and get the list item in the corresponding outer list where it was contained?

Is it possible to check if an item from a list exists in a variable?

Flutter - Check if an index exists in List

How to check if an Integer exists in a list?

Check if an item exists with DynamoDbContext?

Python - Check if part of a string exists in a list and give the index of its match

How to do a loop to check if item exists in firebase?

how to check how many times an item exists in list in c#?

How to check if an item in an object's string list also exists in other string lists in different objects with the same identifier?

How to check if string already exists in list?

How to check if list item exists in list

how to check for a string value if it exists in a list of dictionary (Javascript)?

How to check if a variable exists in a list but ignore it if its not in numerical order?

How to check if an index before a list item exists?

How to Check if an item exists in list in python?

How to check if a string exists in a list of strings in case statement in mysql?

rego check if item in list exists in item in another list

How can I check if hyphen or @ exists in string and get it?

How can I check if a string exists in a dictionary inside of a list in python?

TOP Ranking

HotTag

Archive