且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# 字符串中字符的第三个索引

更新时间:2023-02-06 19:37:15

String.IndexOf 将获得第一个的索引,但有提供起点的重载.所以你可以使用第一个 IndexOf 的结果加上一个作为下一个的起点.然后只累积足够次数的索引:

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);

添加错误处理,除非您知道 myString 至少包含三个冒号.

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