获取C#中

时间:2016-01-08 13:55:19

标签: c# indexing substring

我想在

中获得 xx 的首字母索引
  

xx INC FFFFFFFF xx DE RRRRRR

方法IndexOf没有帮助。 任何线索?

1 个答案:

答案 0 :(得分:1)

您可以使用这样的扩展方法,它使用支持起始索引的overload of String.IndexOfStringComparison可用于比较不区分大小写:

public static IList<int> AllIndexOf(this string text, string str, StringComparison comparisonType = StringComparison.CurrentCulture)
{
    IList<int> allIndexes = new List<int>();
    int index = text.IndexOf(str, comparisonType);
    while (index != -1)
    {
        allIndexes.Add(index);
        index = text.IndexOf(str, index + str.Length, comparisonType);
    }
    return allIndexes;
}

然后很容易:

string str = "xx INC FFFFFFFF xx DE RRRRRR";
IEnumerable<int> allIndexes = str.AllIndexOf("xx", StringComparison.InvariantCulture); // 0 and 16