How To Ideas | How To Articles | How To Tutorials


0

How To Search A String Within Another String In C#


If you want to search a string within another string and perform steps accordingly, then this article is for you. In .NET you have a function IndexOf which returns the index of first occurrence of a character or a string within another string.

Instructions:

  1. If you just want to check whether a string or character is present other string or not, then you can check this using Contains function present in String class. In following example, I will search for word “Ideas” in the  string1, if it founds then it will output about this in Console otherwise it will output that “Ideas” doesn’t exists in string.

    string string1 = "How To Ideas - HowToIdeas";
    if (string1.Contains("Ideas"))
    {
             Console.WriteLine("Our String contains word Ideas");
    }
    else
    {
             Console.WriteLine("Our String do not contain word Ideas");
    }

  2. Note that, in above example, if you try to search for string “ideas” not “Ideas”, then it will not succeed, because this searching is case sensitive, if you want to eliminate this, then you can try the following code.

    if (string1.ToLower().Contains("ideas".ToLower()))
    {
        Console.WriteLine("Our String contains word ideas");
    }
    else
    {
        Console.WriteLine("Our String do not contain word ideas");
    }

  3. Or you can also try the following code

    if (string1.ToUpper().Contains("IDEas".ToUpper()))
    {
        Console.WriteLine("Our String contains word IDEas");
    }
    else
    {
        Console.WriteLine("Our String do not contain word ideas");
    }

  4. If you also want to find out index of a string or character within another string, then you can try the following code.

    string string1 = "How To Ideas - HowToIdeas";
    string string2 = "How";

    int index = string1.IndexOf(string2);

  5. In above example, variable index will contain the index of string word “How” in string1 which is 0. But our string1 also contains one more occurrence of word “How”. If you want to want to find out index of that word, then you have to tell .NET that you want to start searching from index more than index of 1st occurrence of word “How” which is 0. So just start searching from index 1, and .NET will return you the index of 2nd occurrence of word “How” in the given string. here is my code for that.

    int index2 = string1.IndexOf(string2, string1.IndexOf(string2) + 1);

  6. Above code will return you the index of 2nd occurrence of word “How” in string1.

Here you can download full project on searching strings within other string. Download Here.

Here is snapshot of Console window of the project which you can download from the above link.

Search String Or Character In A String C#

If you want to search all the occurrences of a string within another string then see my article on How To Find All The Occurrences Of A String Within Another String In C#.

If you want to check whether a string start or ends with specified string then you can read my article on How To Check Whether A String Starts Or Ends With Specified String.

Incoming search terms:

  • C# search string (3)
  • character exists in string in c# (1)
  • how to get the first character of a string in c# (1)
  • how to scan check on search the character \$\ in asp net c# (1)
Filed in: C# Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

Submit Comment



© 0442 How To Ideas. All rights reserved.
Proudly designed by Theme Junkie.