How to get INDEX of word inside a string

后端 未结 2 1174
谎友^
谎友^ 2021-01-12 08:16

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.

相关标签:
2条回答
  • 2021-01-12 08:45

    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

    0 讨论(0)
  • 2021-01-12 08:47

    Yes, you can use Substring to delete all the characters previous

    string str = "I'm stuck here please help, something else....yyyy";
    string output = str.Substring(str.IndexOf("help"));
    WriteLine($"Output string value  :{output}");//help,something else....yyyy
    
    0 讨论(0)
提交回复
热议问题