Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get INDEX of word inside a string

Tags:

string

c#

I am trying to get the INDEX of a word inside a string and if possible also to delete all the characters previous to this word in the string, it would make my life easier.

Could anyone help me? Im doing this on c#

like image 249
Bryan Arbelo - MaG3Stican Avatar asked Mar 23 '13 01:03

Bryan Arbelo - MaG3Stican


1 Answers

You'll want to use the IndexOf function on a string. This will tell you the starting character position of the word, character, etc that you're looking for.

Here is an example console app:

    static void Main(string[] args)
    {
        String testing = "text that i am looking for";
        Console.Write(testing.IndexOf("looking") + Environment.NewLine);
        Console.WriteLine(testing.Substring(testing.IndexOf("looking")));

        Console.ReadKey();

    }

This will output:
15
looking for

like image 118
Travis Sharp Avatar answered Oct 22 '22 13:10

Travis Sharp