如何使用 indexof 和 substring 从字符串中提取数字并制作数字列表 <int>?
How can I use indexof and substring to extract numbers from a string and make a List<int> of the numbers?
var file = File.ReadAllText(@"D:\localfile.html");
int idx = file.IndexOf("something");
int idx1 = file.IndexOf("</script>", idx);
string results = file.Substring(idx, idx1 - idx);
结果中的结果是:
arrayImageTimes.push('202110071730');arrayImageTimes.push('202110071745');arrayImageTimes.push('202110071800');arrayImageTimes.push('202110071815');arrayImageTimes.push('202110071830');arrayImageTimes.push('202110071845');arrayImageTimes.push('202110071900');arrayImageTimes.push('202110071915');arrayImageTimes.push('202110071930');arrayImageTimes.push('202110071945');
我需要提取“和”之间的每个数字并将每个数字添加到列表
例如:提取号码 202110071730 并将此号码添加到列表
您可以先按;
拆分得到语句列表。
然后将每个语句拆分为 '
以获得 '
之前、之间和之后的所有内容。拿中间那个([1]
).
string s = "arrayImageTimes.push('202110071730');arrayImageTimes.push('202110071745');arrayImageTimes.push('202110071800');arrayImageTimes.push('202110071815');arrayImageTimes.push('202110071830');arrayImageTimes.push('202110071845');arrayImageTimes.push('202110071900');arrayImageTimes.push('202110071915');arrayImageTimes.push('202110071930');arrayImageTimes.push('202110071945');";
var statements = s.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var statement in statements)
{
Console.WriteLine(statement.Split('\'')[1]); // add to a list instead
}
或者,对于所有 Regex 粉丝,'(\d+)'
捕获一个介于 '
和一些数字之间的组:
Regex r= new Regex("'(\d+)'");
var matches = r.Matches(s);
foreach (Match match in matches)
{
Console.WriteLine(match.Groups[1].Value); // add to a list instead
}
var file = File.ReadAllText(@"D:\localfile.html");
int idx = file.IndexOf("something");
int idx1 = file.IndexOf("</script>", idx);
string results = file.Substring(idx, idx1 - idx);
结果中的结果是:
arrayImageTimes.push('202110071730');arrayImageTimes.push('202110071745');arrayImageTimes.push('202110071800');arrayImageTimes.push('202110071815');arrayImageTimes.push('202110071830');arrayImageTimes.push('202110071845');arrayImageTimes.push('202110071900');arrayImageTimes.push('202110071915');arrayImageTimes.push('202110071930');arrayImageTimes.push('202110071945');
我需要提取“和”之间的每个数字并将每个数字添加到列表
例如:提取号码 202110071730 并将此号码添加到列表
您可以先按;
拆分得到语句列表。
然后将每个语句拆分为 '
以获得 '
之前、之间和之后的所有内容。拿中间那个([1]
).
string s = "arrayImageTimes.push('202110071730');arrayImageTimes.push('202110071745');arrayImageTimes.push('202110071800');arrayImageTimes.push('202110071815');arrayImageTimes.push('202110071830');arrayImageTimes.push('202110071845');arrayImageTimes.push('202110071900');arrayImageTimes.push('202110071915');arrayImageTimes.push('202110071930');arrayImageTimes.push('202110071945');";
var statements = s.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var statement in statements)
{
Console.WriteLine(statement.Split('\'')[1]); // add to a list instead
}
或者,对于所有 Regex 粉丝,'(\d+)'
捕获一个介于 '
和一些数字之间的组:
Regex r= new Regex("'(\d+)'");
var matches = r.Matches(s);
foreach (Match match in matches)
{
Console.WriteLine(match.Groups[1].Value); // add to a list instead
}