Searching Strings

Searching strings have been made easy thanks to the different methods provided by .NET. Let’s take a look at the different methods to find the occurrence of a particular string.

The IndexOf and LastIndexOf methods find the index or position of a particular string if it exists in another string. If the given string is not found, the methods will return -1. The example below shows how to use the IndexOf method.

string str = "The quick brown fox jumps over the lazy dog.";
int index = str.IndexOf("quick");

Console.WriteLine(str);
Console.WriteLine("quick was found at position " + index);
The quick brown fox jumps over the lazy dog.
quick was found at position 4

IndexOf accepts a string which is what you are trying to find. In the example above, the method returns 4 because the first occurrence of the word “quick” starts at index 4. Remember that indexes/positions start with 0 to length – 1 so the fifth character is at position 4.

Let’s look at the difference of LastIndexOf by looking at an example.

string str = "A very very very good day.";
int index = str.LastIndexOf("very");

Console.WriteLine(str);
Console.WriteLine("Last occurrence of very was found at position " + index);
A very very very good day.
Last occurrence of very was found at position 12

LastIndexOf is similar the IndexOf except that it finds that index of the last occurrence of the string. In the example above, the last occurrence of the word “very” was found at index 12.

Let’s demonstrate that if a particular search string is not found, the methods will return -1.

string str1 = "This is a sample string.";

Console.WriteLine(str1);

if (str1.IndexOf("Whatever") == -1)
{
    Console.WriteLine("\"Whatever\" was not found in the string.");
}
This is a sample string.
"Whatever" was not found in the string.

Another overloaded version of the methods accepts a second argument that determines where to start searching the search string. This is demonstrated by the code below.

string str1 = "This is a sample string.";

Console.WriteLine(str1);

if (str1.IndexOf("This", 5) == -1)
{
    Console.WriteLine("\"This\" was not found in the string.");
}

You can see that the word “This” is actually in the sentence but we indicated to start at index 5 when searching the word therefore, the word was not found.

If you want to find all the index for every occurrence of a word you can create a loop like this:

int position = 0;
string str1 = "This is a long long long string...";
do
{
    position = str1.IndexOf("long", position);

    if (position != -1)
        Console.WriteLine(position);

    position++;
} while (position > 0);
10
15
20

The Contains method can also be used to check if the string exists in another string.

string str1 = "This is a sample string.";

Console.WriteLine(str1);

if (str1.Contains("sample"))
{
   Console.WriteLine("\"sample\" exists in the string.");
}
This is a sample string.
"sample" exist in the string.

The Contains method returns true if the search string does exist, and false if it does not.

Another pair of methods, StartsWith() and EndsWith() are used to find if a string begins or ends with a particular string.

string str1 = "Apple";

Console.WriteLine(str1);

if (str1.StartsWith("A"))
{
    Console.WriteLine("The word starts with A.");
}
if (str1.EndsWith("e"))
{
    Console.WriteLine("The word ends with e.");
}
Apple
The word starts with A.
The word ends with e.