C# list<string> 怎么提取需要的内容并存入其他数组
例如list<string>s里有从文本里读取的“X100.013Y111.084Z1.07050”“X100.049Y110.931Z1.07569”“X100.191...
例如list<string>s里有从文本里读取的
“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”
怎么把其中的X\Y坐标存入二维数组呢? 展开
“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”
怎么把其中的X\Y坐标存入二维数组呢? 展开
展开全部
用正则表达式来实现
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());
}
}
}
}
展开全部
我也来凑凑热闹,试著用Linq,感觉不错哦
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqTest1
{
struct DoublePoint
{
private double x;
private double y;
public DoublePoint(double[] a)
{
this.x = a[0];
this.y = a[1];
}
public double X
{
get { return this.x; }
set { this.x = value; }
}
public double Y
{
get { return this.y; }
set { this.y = value; }
}
}
class Program
{
static void Main(string[] args)
{
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"};
var a = from item in list select new DoublePoint(Array.ConvertAll(item.Replace("X", "").Replace("Y", "").Substring(0, item.IndexOf(" Z") - 2).Split(' '), Double.Parse));
DoublePoint[] value = a.ToArray();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这些里的这些数字那么整齐还能不好存么,用一个for循环或者foeach循环,把这些数据全部遍历一遍,因为你这些数字过于整齐了,所以你完全可以拿出一项之后 s[i].substring(1,7),这样就拿出x了,同样s[i].substring(10,7)就拿出y了,因为你拿出来是string,记得再Convet.Double一下,就行了,拿出来的坐标你可以去定义一个二维数组都给放进去就行了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
List<double[]> list=new List<double[]>();
foreach(string str in s)
{
string[] arr=str.Split(new char[]{' '},StringSplitOption.RemoveEmptyEntries);
if(arr.Length>=2)
{
doube x=Convert.ToDouble(arr[0].Trim('X'));
doube y=Convert.ToDouble(arr[1].Trim('Y'));
list.Add(new double[]{x,y});
}
}
//list就是你要的结果了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
float[] getzXYZ(string t)
{
if (t.IndexOf("X")<0 ||t.IndexOf("Y")<0||t.IndexOf("Z")<0)
{
return null;
}
string sx = t.Substring(t.IndexOf("X"), t.IndexOf("Y") - t.IndexOf("X")).Substring(1);
string sy = t.Substring(t.IndexOf("Y"), t.IndexOf("Z") - t.IndexOf("Y")).Substring(1);
string sz = t.Substring(t.IndexOf("Z")).Substring(1);
float x = float.Parse(sx);
float y = float.Parse(sy);
float z = float.Parse(sz);
float[] rtn=new float[3];
rtn[0]=x;
rtn[1]=y;
rtn[2]=z;
return rtn;
}
//自己迭代
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询