Get Index of First non-Whitespace Character in C# String

后端 未结 11 1880
执念已碎
执念已碎 2020-12-17 09:27

Is there a means to get the index of the first non-whitespace character in a string (or more generally, the index of the first character matching a condition) in C# without

11条回答
  •  萌比男神i
    2020-12-17 10:10

    A string is of course an IEnumerable so you can use Linq:

    int offset = someString.TakeWhile(c => char.IsWhiteSpace(c)).Count();
    

提交回复
热议问题