我想分开这个:
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++;
}
}
答案 0 :(得分:4)
words = poly[a].Split(charseparators, StringSplitOptions.RemoveEmptyEntries);
使用Split
的{{1}}重载将确保它将删除所有空数组元素。
这取决于StringSplitOptions.RemoveEmptyEntries
的类型,如果它是charseparators
的数组,则重载运算符将可用。如果不是,你只需要把它放在一个:
char
有趣的是:
words = poly[a].Split(new [] { charseparators }, StringSplitOptions.RemoveEmptyEntries);
此测试返回结果:
var str = "0, 250, 6, 5000, 10000, 15000, 20000, 25000, 70, 70, 70, 70, 70, 70, 0,";
var timer = System.Diagnostics.Stopwatch.StartNew();
for (var i = 0; i < 1000000; i++)
{
str.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
}
timer.Stop();
Console.WriteLine($"Spliting took: {timer.ElapsedMilliseconds}ms");
timer = Stopwatch.StartNew();
for (int i = 0; i < 1000000; i++)
{
str.Trim(',').Split(',');
}
timer.Stop();
Console.WriteLine($"Trimming then Spliting took: {timer.ElapsedMilliseconds}ms");
只有&gt; 显着 10,000结果的结果是:
Spliting took: 810ms
Trimming then Spliting took: 570ms
答案 1 :(得分:0)
您可以添加此行以删除\r
和\n
if(words.Length > 0)
richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.Text.Lenght - 2, 2);
答案 2 :(得分:0)
如果您的字符串中允许空语句,则无法使用
words = poly[a].Split(charseparators, StringSplitOptions.RemoveEmptyEntries);`
因为这会删除这些条目。我会在最后修剪分离器。
words = poly[a].Split(charseparators.TrimEnd(','));
答案 3 :(得分:0)
通过StringSplitOptions.RemoveEmptyEntries
删除空条目:
richTextBox1.Text = string.Join(Environment.NewLine, poly[a]
.Split(charseparators, StringSplitOptions.RemoveEmptyEntries)
.Select((value, index) => String.Format("{0} {1}", index + 1, value.Trim())));
要阻止闪烁的richTextBox1
,请尝试避免
richTextBox1.Text += ... // <- this is a bad parctice
但在一次中指定文本值(例如,通过string.Join
)