下面代码的输出是什么? [英] What would be the output of the folowwing code
本文介绍了下面代码的输出是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 System.Text.RegularExpressions;
string str = <多边形>
< Vertex x = 9352 。 7606 < span class =code-string> y = 8250 。 6001 z = 505 。 3871 />
< Vertex x = 9352 。 7573 y = 8250 。 6001 z = 505 。 3844 />
< / Polygon>;
var result = Regex.Split(str, \ r \ n | \ r | \ n);
列表< string> lineString = new 列表< string>();
for ( int count = 0 ; count< result.Length; count ++)
{
lineString.Add(result [count]);
}
MatchCollection matches = Regex.Matches(lineString, @ \d +(\.\d +)?);
列表< double> numbers = 新列表< double>();
foreach (匹配m matches)
{
numbers.Add( Double .Parse(m.Value, new System.Globalization.NumberFormatInfo(){NumberDecimalSeparator = 。}));
}
StringBuilder sb = new StringBuilder();
foreach ( double newNumbers in numbers)
{
sb.Append(numbers [newNumbers] .ToString()+ \t跨度>);
}
string finalResult = sb.ToString();
Console.WriteLine(finalResult);
解决方案
编译错误:很多。
您的字符串str
行将无法编译...
尝试:
string str = @ < Polygon>
< Vertex x =9352.7606y =8250.6001z =505.3871/>
< Vertex x =9352.7573y =8250.6001 z =505.3844/>
< /多边形>;
然后,你将进入下一个编译器错误:
MatchCollection matches = Regex.Matches(lineString, @ \d +(\.\d +)?);由于
lineString
是一个字符串列表,并没有Regex.Matches方法列出...
加油!这并不复杂:我昨天给你了90%如何仅从字符串中提取数字 [ ^ ] - 你要做的就是思考一下,并在它周围添加一些代码......
using System.Text.RegularExpressions;
string str = "<Polygon>
<Vertex x="9352.7606" y="8250.6001" z="505.3871" />
<Vertex x="9352.7573" y="8250.6001" z="505.3844" />
</Polygon>";
var result = Regex.Split(str, "\r\n|\r|\n");
List<string> lineString=new List<string>();
for(int count=0;count<result.Length;count++)
{
lineString.Add(result[count]);
}
MatchCollection matches = Regex.Matches(lineString, @"\d+(\.\d+)?");
List<double> numbers = new List<double>();
foreach (Match m in matches)
{
numbers.Add(Double.Parse(m.Value, new System.Globalization.NumberFormatInfo() { NumberDecimalSeparator = "." }));
}
StringBuilder sb=new StringBuilder();
foreach(double newNumbers in numbers)
{
sb.Append(numbers[newNumbers].ToString()+"\t");
}
string finalResult=sb.ToString();
Console.WriteLine(finalResult);
解决方案
Compiler error: lots of.
Yourstring str
line will not compile...
Try:
string str = @"<Polygon> <Vertex x=""9352.7606"" y=""8250.6001"" z=""505.3871"" /> <Vertex x=""9352.7573"" y=""8250.6001"" z=""505.3844"" /> </Polygon>";
And then, you will get to the next compiler error:
MatchCollection matches = Regex.Matches(lineString, @"\d+(\.\d+)?");Since
lineString
is a List of strings, and there is no Regex.Matches method that takes a list...
Come on! This isn't complicated: I gave you 90% of this yesterday How to extract only numbers from a string[^] - all you have to do is think a little, and add a bit of code around it...
这篇关于下面代码的输出是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文