C#

C# | Searching Strings |String.IndexOf( ) Method-Trickcode

Searching strings,C# | Searching Strings |String.IndexOf( ) Method-Trickcode.,IndexOf and LastIndexOf
Share it:
Searching Strings

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 = "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);
Console.ReadLine();

A very very very good day.
Last occurrence of very was found at position 12

LastIndexOf is similar to 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.");
    Console.ReadLine();
}

This is a sample string.
"Whatever" was not found in the string.
Another an 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.");
   Console.ReadLine();
}

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);
        Console.ReadLine();

    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.");
   Console.ReadLine();
}
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.");
   Console.ReadLine();
}
Apple
The word starts with A.
The word ends with e.
Share it:

C#

Post A Comment:

0 comments: