假设我有一段文本,我想找到每个逗号的位置。字符串的简短版本如下:
string s = "A lot, of text, with commas, here and,there";
理想情况下,我会使用类似以下的东西:
int[] i = s.indexOf(',');
但是,由于indexOf只返回第一个逗号,因此我改为执行以下操作:
List<int> list = new List<int>();
for (int i = 0; i < s.Length; i++)
{
if (s[i] == ',')
list.Add(i);
}
有没有其他更优化的做法?