Get index of first detected space after a certain index in a string

前端 未结 2 1253
栀梦
栀梦 2021-01-17 13:33

In a string to format (mostly to replace chars with different symbols for rendering test on UI), I have to detect % and then skip all chars util first space from this % char

相关标签:
2条回答
  • 2021-01-17 14:01

    Following is easiest, and extensible, for your requirement "abcd%1$s efgh %2$d ijkl in this string I have to skip this %1$s & %2$d which are some sort of formatting placeholders."

    string[] placeHolders = new string[] {"%1$s", "%2$d"};
    string[] splits = "abcd%1$s efgh %2$d ijkl".Split(placeHolders, StringSplitOptions.None);
    

    which will provide splits as ["abcd", "efgh", "ijkl"]

    0 讨论(0)
  • 2021-01-17 14:18

    You can get that pretty easily, just grab the index if the first percent sign and then leverage that index to find the first space from there:

    var start = myString.IndexOf("%");
    var spaceIndex = myString.IndexOf(" ", start)
    

    of course the value of myString is the string you represented in your question.

    0 讨论(0)
提交回复
热议问题