C# third index of a character in a string

前端 未结 10 1951
甜味超标
甜味超标 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 18:15

    A little ugly, but an alternative approach (to the others already posted) that works:

    public int FindThirdColonIndex(string msg)
    {       
        for (int i = 0, colonCount = 0; i < msg.Length; i++)
        {
            if (msg[i] == ':' && ++colonCount == 3) { return i; }
        }
    
        // Not found
        return -1;
    }
    

提交回复
热议问题