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

后端 未结 11 1838
执念已碎
执念已碎 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条回答
  •  醉梦人生
    2020-12-17 10:12

    Something is going to be looping somewhere. For full control over what is and isn't whitespace you could use linq to objects to do your loop:

    int index = Array.FindIndex(
                   s.ToCharArray(), 
                   x => !(new [] { '\t', '\r', '\n', ' '}.Any(c => c == x)));
    

提交回复
热议问题