c# - 如何使用 indexof 和 substring 从字符串中提取数字并制作列表的数字?
问题描述
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 并将此号码添加到 List
解决方案
您可以首先拆分;
以获取语句列表。
然后将每个语句拆分'
以获取 . 之前、之间和之后的所有内容'
。取中间的([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
}
推荐阅读
- sql - 如何使用上一列中的值插入日期
- python - 为什么我们将 nn.Module 作为参数传递给神经网络的类定义?
- javascript - 大端格式到十进制转换
- javascript - 使用 ReactJS 上传旋转图像预览
- r - 如何将模型系数乘以一个数字?
- json - Chrome 和 Firefox 扩展:在哪里存储数据?
- sql - 如何在 ASP.NET MVC 和 SQL Server 中修复“对象在 Null 时应具有值”
- reactjs - 当我在visualstudio代码中保存js文档时创建空间
- hadoop - 无法使用 Java 代码连接到 Hive
- reactjs - 获取 TypeError: _react.default.memo 仅当我在我的应用程序中使用 FavoriteIcon 时才不是一个函数