用正则表达式来实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
//定义一个结构存放X,Y坐标
struct MyPoint
{
public float x;
public float y;
public override string ToString()
{
return string.Format("x={0}, y={1}", x, y);
}
}
static void Main(string[] args)
{
//存放提取出的坐标点
List<MyPoint> pointList = new List<MyPoint>();
List<string> list = new List<string>() {
"X100.013 Y111.084 Z1.07050",
"X100.049 Y110.931 Z1.07569",
"X100.191 Y110.652 Z1.08603",
"X100.412 Y110.431 Z1.09638",
"X100.691 Y110.289 Z1.10673",
"X100.844 Y110.253 Z1.11192",
"X101.008 Y110.240 Z1.11737"
};
//匹配模式串
string pattern = @"X(?<x>\d+.\d+) Y(?<y>\d+.\d+) Z\d+.\d+";
foreach (string s in list)
{
Match match = Regex.Match(s, pattern);
if (match.Success)
{
//从配合结果中取出x,y,并转换成浮点数
float xV = float.Parse(match.Result("${x}"));
float yV = float.Parse(match.Result("${y}"));
//存放到集合中
pointList.Add(new MyPoint() { x = xV, y = yV });
}
}
// 结果数组
MyPoint[] points = pointList.ToArray();
// 显示提前结果
for (int i = 0; i < points.Length; i++)
{
Console.WriteLine(points[i].ToString());
}
}
}
}