C# third index of a character in a string

前端 未结 10 1972
甜味超标
甜味超标 2020-12-06 17:18

is there a command that can get the third index of a character in a string? For example:

error: file.ext: line 10: invalid command [test:)]

10条回答
  •  天涯浪人
    2020-12-06 17:54

    String.IndexOf will get you the index of the first, but has overloads giving a starting point. So you can use a the result of the first IndexOf plus one as the starting point for the next. And then just accumulate indexes a sufficient number of times:

    var offset = myString.IndexOf(':');
    offset = myString.IndexOf(':', offset+1);
    var result = myString.IndexOf(':', offset+1);
    

    Add error handling unless you know that myString contains at least three colons.

提交回复
热议问题