In this article, we are going to see the different ways we can check for a specific value in a string array and return the index of that value.

We will focus mainly on linear search methods. We cannot use binary search methods since we do not know if the array is sorted and cannot assume it is. All the methods we will explore share a certain similarity: they will either return the index of the value or -1 if it is not present.

Then, we will compare the speed and memory allocation of all the methods that return the index of the first occurrence of a particular string using some benchmarks at the end of the article.

Let’s dive in.

## Prepare the Environment

First, let’s define a `string` array and populate it with some strings:

```private readonly string[] _stringArray = { "dog", "horse",
"emu", "lizard", "duck", "frog", "SNAKE", "emu", "duck", "GOOSE" };```

We will use this array as the data source for all the methods that we will implement.

Now, let’s see the different ways we can search for a string in a string array and get its index.

## Use Iteration to Check if a String Array Contains a Value

In this section, we will explore how to use two iteration approaches to find an item in an array and obtain its index.

• for
• foreach

### Use a for Loop

First, let’s use a `for` loop to iterate through the string array, find the specified value, and return its index:

```public int ForLoop()
{
string value = "horse";
int index = -1;
for (int i = 0; i < _stringArray.Length; i++)
{
if (_stringArray[i] == value)
{
index = i;
break;
}
}

return index;
}```

First, we initialize a variable `index` equal to -1. Then, during each iteration, we assign the value of `i` to `index`. If the current element at the index `i` is the same as the value we are searching for, we exit the loop.

Finally, we return the value of `index`.

### Use a foreach Loop

Alternatively, we can use a `foreach` loop to perform the same operation:

```public int ForEachLoop()
{
string value = "dog";
int index = -1;
int step = 0;
foreach (var item in _stringArray)
{
if (item == value)
{
index = step;
break;
}
step++;
}

return index;
}```

In this method, we set the `index` variable to -1 and the `step` variable to 0. Then, we iterate through all the elements in the array. At each iteration, we increase the value of `step` by 1. The loop terminates after the variable `index` is assigned with the value of `step` if it finds an element that matches the variable `value`, then we return the value of `index`. The variableÂ `index` represents the index of that element in the array. If the loop is completed without finding such an element, the variable `index` will be -1.

## Array Class Methods to Check if a String Array Contains a Value

In this section, we will look at some relevant methods in the `Array` class that we can use to perform this task:

• IndexOf()
• FindIndex()
• FindLastIndex()
• LastIndexOf()

### Use the IndexOf() Method From the Array Class

Now, let’s use the `IndexOf()` method of the `Array` class to check for a value in a string array and return its index:

```public int ArrayIndexOf()
{
string value = "lizard";
int index = Array.IndexOf(_stringArray, value);

return index;
}```

Here, we pass two arguments (the array we want to search and the value to search for) to the `IndexOf()` method. This method returns the index of the first occurrence of `value`, or -1 if it is not found.

### Use the FindIndex() Method From the Array Class to Find an Exact Match

Next, let’s implement a method to see how we can use the `Array.FindIndex()` method to perform this operation:

```public int ArrayFindIndex()
{
string value = "frog";
int index = Array.FindIndex(_stringArray, str => str == value);

return index;
}```

Here, we invoke the `FindIndex()` method. This method takes in two arguments: the array, and a predicate. It checks `_stringArray` for the first occurrence of any string that satisfies the condition defined by the predicate and returns its index. If no such element exists in the array, it returns -1.

Now, let’s see how we can use the `FindIndex()` method to perform a case-insensitive search for a value in a string array, and then return its index:

```public int ArrayCaseInsensitiveFindIndex()
{
string caseInsensitiveValue = "Snake";
int index = Array.FindIndex(_stringArray,
str => str.Equals(caseInsensitiveValue,
StringComparison.OrdinalIgnoreCase));

return index;
}```

Again, we pass in two arguments to the `FindIndex()` method and invoke it. But this time, the condition specified by the predicate is different.

In the predicate function, we call the `Equals()` method of the `string` class to compare the elements of our array with a case-insensitive value. Then, we pass in the `StringComparison.OrdinalIgnoreCase` option to specify that an ordinal sort rule should be used to compare the strings. We use this particular `StringComparison` member because it is fast and culture-agnostic. With this, we ignore the case of the characters in the strings when we compare them.

Finally, if a string in the array matches the condition we specified in the predicate, we return its index. If no match exists in the array, we return -1. Since we try to find the value `Snake`, the string `SNAKE` in the array will be matched.

### Use the FindIndex() Method From the Array Class to Find a Partial Match

Now, we will demonstrate how we can use the `FindIndex()` method to locate a partially matched value and return its index:

```public int ArrayFindIndexWithContains()
{
string value = "du";
int index = Array.FindIndex(_stringArray, str => str.Contains(value));

return index;
}```

This method returns the index of the first string that contains the value `du`. Here, the predicate takes a string parameter and returns a boolean indicating whether the `str` contains the string `du`.

Finally, we return the index of the first occurrence of the string `duck` that matches the predicate’s condition.

### Use StartsWith, EndsWith, and Regex For Partial Match

Furthermore, we can use the `StartsWith()` method, the `EndsWith()` method, or even a regex match in the predicate function to perform a partial search.

Using the `StartsWith()` method:

```public int ArrayFindIndexWithStartsWith()
{
string value = "liz";
int index = Array.FindIndex(_stringArray, str => str.StartsWith(value));

return index;
}```

Here, the `FindIndex()` method searches the array for the first element that starts with `liz` and returns the index of that element. If no such element is found, `FindIndex()` returns -1.

Now, let’s see how we can use the `EndsWith()` method in our predicate function:

```public int ArrayFindIndexWithEndsWith()
{
string value = "rse";
int index = Array.FindIndex(_stringArray, str => str.EndsWith(value));

return index;
}```

In this scenario, we invoke the `FindIndex()` method to search the array for the first element that ends with `rse` and returns its index. If no such element is found, `FindIndex()` returns -1.

Next, let’s demonstrate how we can use a regex to check for a partial match:

```public int ArrayFindIndexWithRegex()
{
string pattern = @"^(fro)";
int index = Array.FindIndex(_stringArray, str => Regex.IsMatch(str, pattern));

return index;
}```

Here, we define a regex pattern that the desired string should start with. Then, theÂ  `FindIndex()` method searches the array for the first element that matches that pattern and returns its index. If no such element is found, it returns -1.

### Use the LastIndexOf() Method From the Array Class

Additionally, we can find the index of the last occurrence of a value in a string array by using the `LastIndexOf()` method:

```public int ArrayLastIndexOf()
{
string value = "duck";
int index = Array.LastIndexOf(_stringArray, value);

return index;
}```

Here, we call the `LastIndexOf()` method to get the index of the last desired string in the array. It returns -1 if the item is not in the array.

### Use the FindLastIndex() Method From the Array Class

Now, let’s use the `FindLastIndex()` method to get the index of the last occurrence of a particular string:

```public int ArrayFindLastIndex()
{
string value = "emu";
int index = Array.FindLastIndex(_stringArray, str => str == value);

return index;
}```

We invoke the `FindLastIndex()` method by passing in the array and a predicate as arguments. The method returns the index of the last string within the array that satisfies the predicate’s condition. In this case, we will return the index of the last occurrence of the string `emu`.

## Benchmark All the Methods

We have implemented a benchmark class to measure the time and memory performance of each method that can retrieve the index of the first string in a case-sensitive search through a `string` array. Visit here to see how to use this package.

Now, let’s inspect our `CheckMethodsBenchmark` class implementation:

```[MemoryDiagnoser]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
public class CheckMethodsBenchMark
{
private readonly string[] _stringArray = GenerateBenchMarkArray.CreateArray();
private const string Value = "jane doe";

[Benchmark]
public int ForLoop()
{
int index = -1;
for (int i = 0; i < _stringArray.Length; i++)
{
if (_stringArray[i] == Value)
{
index = i;
break;
}
}

return index;
}

[Benchmark]
public int ForEachLoop()
{
int index = -1;
int step = 0;
foreach (var item in _stringArray)
{
if (item == Value)
{
index = step;
break;
}
step++;
}

return index;
}

[Benchmark]
public int ArrayIndexOf()
{
int index = Array.IndexOf(_stringArray, Value);

return index;
}

[Benchmark]
public int ArrayFindIndex()
{
int index = Array.FindIndex(_stringArray, str => str == Value);

return index;
}
}```

This benchmark will run using an array of 1000 strings.

Now, let’s run our program and inspect the results:

```|         Method |      Mean |     Error |    StdDev |    Median | Allocated |
|--------------- |----------:|----------:|----------:|----------:|----------:|
|    ForEachLoop |  3.680 us | 0.1033 us | 0.3046 us |  3.851 us |         - |
|        ForLoop |  4.467 us | 0.0984 us | 0.2885 us |  4.556 us |         - |
| ArrayFindIndex | 10.163 us | 0.2934 us | 0.8652 us | 10.639 us |         - |
|   ArrayIndexOf | 11.905 us | 0.2572 us | 0.7421 us | 12.172 us |         - |```

As we can see, using a `foreach` loop is the fastest way to search for a string in a string array and return its index with a mean time of 3.680 microseconds. The `ForLoop()` method is slightly slower, with a mean time of 4.467 microseconds. The `ArrayFindIndex()` and `ArrayIndexOf()` methods are much slower, with mean times of 10.163 and 11.905 microseconds.

Furthermore, we can see that all the methods compared do not allocate any memory and do not use the garbage collector.

## Conclusion

In this article, we have explored the different ways we can check for a value in a string array and get its index. Also, we’ve learned that each method offers a different performance, so it is important for us to always measure the performance of our code to get insights. Understanding how each method works allows us to choose the most efficient method that meets our specific needs.