【发布时间】:2020-12-24 20:07:34
【问题描述】:
我正在尝试使用此辅助方法从字符串中的每个索引创建子字符串
public List<int> AllIndexesOf(string str, string value)
{
if (String.IsNullOrEmpty(value))
throw new ArgumentException("the string to find may not be empty", "value");
List<int> indexes = new List<int>();
for (int index = 0; ; index += value.Length)
{
index = str.IndexOf(value, index);
if (index == -1)
return indexes;
indexes.Add(index);
}
}
而我的使用方式是这样的
string input = "27758585926302004842";
List<int> eh = AllIndexesOf(input, "2");
所以我基本上想在两个索引之间抓取每个字符串。
因此,索引 0 和 9 以及 13 和 19 之间的下标。
我只是不确定如何在不使用 linq 的情况下做到这一点
【问题讨论】:
-
考虑使用正则表达式?
-
尝试在没有 linq 和 regex 的情况下执行此操作,如果有意义的话,就像一种纯粹的方式
-
@VargaDev 为什么? LINQ 出了什么问题?
-
我不喜欢 linq,但我只是想尝试在没有任何扩展如 regex 或 linq 的情况下做到这一点
-
我不明白“子字符串”在哪里使用。笨拙的循环只是返回匹配
value的字符的“索引”。您想从str获取哪些“子字符串”?这个评论……“所以我本质上想把索引之间的每个字符串一分为二。”……没有意义。