c# - 我怎样才能删除最后一部分?

标签 c# string split

<分区>

我想分开这个:

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++;


        }





    }

最佳答案

words = poly[a].Split(charseparators, StringSplitOptions.RemoveEmptyEntries);

使用 StringSplitOptions.RemoveEmptyEntriesSplit 重载将确保它会删除所有空数组元素。

这取决于 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");

此测试返回结果:

Spliting took: 810ms
Trimming then Spliting took: 570ms

值得注意仅在 > 10,000 次交互时结果为:

Spliting took: 7ms
Trimming then Spliting took: 5ms

关于c# - 我怎样才能删除最后一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38262349/

相关文章:

c# - 命名约定,C# 异步方法

python - 如何在 Python 中第一次出现字母时拆分字符串?

JavaScript - 将数组分成具有随机数量对象的组

c# - 如何将字符串数组拆分为新的字符串数组并删除重复项

c# - 如何在 C# 项目中使用 Scintilla .NET?

c# - 在按钮上移动时播放声音

python - 用 pandas 搜索并返回匹配子串的索引

javascript url 安全文件名安全字符串

sql - Postgres 使用 `split_part` 索引

c# - 如何确定当前Windows定时器的分辨率?