c# - 如何从文件中读取并将此特定文件文本转换为字典
问题描述
我想从 .txt 文件中读取并将文本转换为 <string, string> 字典。不过,我只想存储 X 和 Y 值,例如 <X, Y>。
我如何使用下面的当前文本文件来解决这个问题?
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=359,Y=154}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=678,Y=230}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=625,Y=171}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=565,Y=314}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=409,Y=262}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=410,Y=59}]
[System.Windows.Forms.PictureBox, SizeMode: Normal {X=777,Y=151}]
解决方案
一种方法是使用正则表达式:
var s = "[System.Windows.Forms.PictureBox, SizeMode: Normal {X=359,Y=154}]";
var reg = new Regex(pattern: "{X=([0-9]*),Y=([0-9]*)}");
var match = reg.Match(s);
if(match.Success)
{
var x = match.Groups[1].Value; // string "359" but you can use int.TrypParse to get a number
var y = match.Groups[2].Value;
}
推荐阅读
- azure - Azure Devops:选择在阶段级别使用哪个变量组
- azure - 使用 Azure ACR API 列出存储库标记
- amazon-web-services - 路由 53 中基于 aws 延迟的路由如何工作?
- c++ - 使用 RcppArmadillo 计算向量的尾和
- java - findViewById 仅对一个元素返回 null
- node.js - Nodemon 服务器无法使用 NPM 启动命令
- node.js - API调用返回的Contract_ABI有JSON接口错误
- java - 无法在 Java 终端中运行多个命令
- c# - C# struct 或 struct[] 作为内存中连续结构的成员
- c# - 从字符串中修剪字符串