【发布时间】:2016-07-08 08:39:12
【问题描述】:
我想拆分这个:
0, 250, 6, 5000, 10000, 15000, 20000, 25000, 70, 70, 70, 70, 70, 70, 0,
我试过了:
words = poly[a].Split(charseparators);
foreach (string word in words)
{
richTextBox1.Text += (d + 1)+ " " + word+ "\r\n";
d++;
}
这不是完整的代码,但问题是:它应该是这样的:
18 70
19 70
20 0
但它看起来像这样:
18 70
19 70
20 0
21
还有一个额外的部分,因为在最后一个单词的末尾,总是有一个 ',' 我怎样才能删除最后一行?
代码:
public void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
int size = -1;
string text = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
file = openFileDialog1.FileName;
try
{
text = File.ReadAllText(file);
size = text.Length;
}
catch (IOException)
{
}
}
int a = 0;
int b =1;
int c = 0;
int d = 0;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(file);
XmlNodeList nodes = xmlDoc.SelectNodes("//Pacs_Parad//Pac_Parameter_Set//Pac_Zuo_Pave_Para");
XmlNodeList polygon = xmlDoc.GetElementsByTagName("Polygon_CS_List");
XmlNodeList value = xmlDoc.GetElementsByTagName("Value");
XmlNodeList synonym = xmlDoc.GetElementsByTagName("Synonym_Name");
XmlNodeList typeflag = xmlDoc.GetElementsByTagName("Type_Flag");
string[] poly = new string[polygon.Count];
foreach(XmlNode node in polygon)
{
poly[a] = node.InnerText;
a++;
}
a = 0;
string[] tf = new string[size];
foreach (XmlNode node in typeflag)
{
tf[a] = node.InnerText;
a++;
}
a = 0;
richTextBox1.Multiline = true;
richTextBox1.Clear();
string[]words = null;
char[] charseparators = new char[] { ',' };
for (int i = 0; i <synonym.Count; i++)
{
richTextBox1.Text += b + "." + " Name: " + synonym[i].InnerText + "\r\n" +
" Type: " ;
if (tf[i] == "P")
{
richTextBox1.Text += "Polygon " + "\r\n";
words = poly[a].Split(charseparators);
foreach (string word in words)
{
richTextBox1.Text += (d + 1)+ " " + word+ "\r\n";
d++;
}
d = 0;
a++;
}
else
{
if (tf[i] == "C")
{
richTextBox1.Text += "Constant " + "\r\n";
richTextBox1.Text += "value: " + value[c].InnerText + "\r\n";
c++;
}
}
richTextBox1.Text += "\r\n";
b++;
}
}
【问题讨论】:
-
拆分前使用
.RTrim(',')怎么样? -
它仍然无法按我的意愿工作,我仍然不明白为什么......
-
“无法按我的意愿工作”是什么意思?你还有多余的条目吗?您是否执行了任何建议?如果是这样:哪个?在哪里?
-
我尝试了你们写的所有这些东西,我真的很感谢,但它们都不起作用,是的,我还有额外的 21 个,但我用我的方式尝试了它,它起作用了words = poly[a].Split(charseparators, StringSplitOptions.RemoveEmptyEntries); foreach (string word in words) { richTextBox1.Text += (d + 1)+ " " + word.Trim(',')+ "\r\n"; d++;
-
正确的修剪方法是在
foreach之前使用words = poly[a].TrimEnd(charseparators).Split(charseparators);