【问题标题】:How can I remove the last part? [duplicate]如何删除最后一部分? [复制]
【发布时间】: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);

标签: c# string split


【解决方案1】:
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

【讨论】:

    【解决方案2】:

    您可以添加此行以删除最后一个字符 \r\n

    if(words.Length > 0)
        richTextBox1.Text = richTextBox1.Text.Remove(richTextBox1.Text.Lenght - 2, 2);
    

    【讨论】:

      【解决方案3】:

      如果您的字符串中允许有空条目,则不能使用

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

      因为这也会删除此类条目。我会在最后修剪分隔符。

      words = poly[a].Split(charseparators.TrimEnd(','));
      

      【讨论】:

        【解决方案4】:

        通过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

        【讨论】:

          猜你喜欢
          • 2010-11-03
          • 1970-01-01
          • 1970-01-01
          • 2017-12-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-05
          • 2015-01-15
          • 2012-09-01
          相关资源
          最近更新 更多