c#中,怎样同时把多行string转换为double

2025-02-07 16:19:42
推荐回答(3个)
回答(1):

        //using System.Linq;
        //using System.Drawing;须添加引用
        static void Main(string[] args)
        {
            string s = @"{lat=-36.123456789,lng=174.12342326789} 
{lat=-36.124346789,lng=174.12334356789} 
{lat=-36.12333329,lng=174.12342326789} 
{lat=-36.123536789,lng=174.1674456789} ";
            var v = s.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => x.Replace("{lat=", "").Replace("lng=", "").Replace("}", ""))
           .Select(x => new
            {
                x,
                xs = x.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
            })
            .Select(x => new PointF(Convert.ToSingle(x.xs[0]), Convert.ToSingle(x.xs[1])));

            foreach (PointF p in v)
                Console.WriteLine("x={0},y={1}", p.X, p.Y);
            Console.ReadLine();
        }

回答(2):

用循环就可以了。

回答(3):

protected void Button1_Click(object sender, EventArgs e)
{
string aa = TextBox1.Text;
for (int i = 0; i <= aa.Split().Length - 1; i++)
{
if (aa.Split()[i].ToString().Trim() != "")
{
GetValue(aa.Split()[i].ToString());
}
}

}
private void GetValue(string strValue )
{
string S1, S2, S = strValue;
string[] x = S.Split(new char[] { ',' });
string y = x[0].ToString();//{lat=-36.12333333
string z = x[1].ToString();//lng=174.333333
S1 = y.Substring(y.IndexOf('=')+1);
S2 = z.Substring(z.IndexOf('=') + 1, z.IndexOf('}') - 5);
double f1 = double.Parse(S1), f2 = double.Parse(S2);
TextBox2.Text += f1.ToString()+" "+f2.ToString() ;
}