删除C#中的前几个数字和空格,重新编号ListBox。

huangapple go评论66阅读模式
英文:

C# delete first numbers and spaces, renumber ListBox

问题

在图片中可以看到,我想要移除编号。有时这些编号是特殊的,你可以看到编号后面有几个空格。在1-9之间通常有3个空格,但有时会有4个。在10-99之间有2个或有时3个。我该如何移除它们并创建新的编号?谢谢。

我尝试过:

private void button43_Click(object sender, EventArgs e)
{
    var empty = " ";
    for (int i = 0; i < listBox1.Items.Count; i++)
    {
        if (listBox1.Items[i].ToString().Contains(empty))
        {
            listBox1.SetSelected(i, true);
            var newString = listBox1.SelectedItem.ToString();
            newString = newString.Substring(4);
            listBox1.Items.Insert(listBox1.SelectedIndex, newString);
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            listBox1.ClearSelected();
        }
    }
}

删除C#中的前几个数字和空格,重新编号ListBox。

英文:

As you can see in the picture, I want to remove the numbering. This is sometimes special and you can see that there are a few spaces after the number. Between 1-9 there are usually 3, but sometimes 4. Between 10-99 there are 2 or sometimes 3. How can I remove them and make a new numbering? Thanks.

I tried:

private void button43_Click(object sender, EventArgs e)
        {
            var empty = &quot; &quot;;
            for (int i = 0; i &lt; listBox1.Items.Count; i++)
            {
                if (listBox1.Items[i].ToString().Contains(empty))
                {
                    listBox1.SetSelected(i, true);
                    var new = listBox1.SelectedItem.ToString();
                    new = new.Substring(4);
                    listBox1.Items.Insert(listBox1.SelectedIndex, new);
                    listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                    listBox1.ClearSelected();
                }
            }
        }

删除C#中的前几个数字和空格,重新编号ListBox。

答案1

得分: 1

你的逻辑有误,因为你正在插入数据,从而将所有内容上移。你需要要么交换删除和插入的顺序,要么在 RemoveAt 上添加1。

更好的方法是就地修改它。

而且你不需要选择任何内容。

if (listBox1.Items[i].ToString().Contains(empty))
{
    listBox1.Items[i] = listBox1.Items[i].ToString().Substring(4);
}
英文:

Your logic is wrong, because you are inserting data, and therefore moving everything up. You would need to either flip the remove and insert, or add 1 to the RemoveAt.

Even better, just modify it in place.

And you don't need to select anything.

if (listBox1.Items[i].ToString().Contains(empty))
{
    listBox1.Items[i] = listBox1.Items[i].ToString().Substring(4);
}

答案2

得分: -1

以下是您要翻译的代码部分:

您可以使用 String.TrimStart(char[]) TrimStart(char[]) 从字符串开头删除所有提到的字符,直到第一个未提到的字符。

if (listBox1.Items[i].ToString().Contains(empty))
{
    listBox1.SetSelected(i, true);
    var new = listBox1.SelectedItem.ToString();
    // 假设 i 是新的数字。
    new = i.ToString() + " " + new.TrimStart(' ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0');
    listBox1.Items.Insert(listBox1.SelectedIndex, new);
    listBox1.Items.RemoveAt(listBox1.SelectedIndex);
    listBox1.ClearSelected();
}

来源:
https://learn.microsoft.com/en-us/dotnet/api/system.string.trimstart?view=net-7.0

我还建议不要将 "new" 用作任何东西的名称。

英文:

You could use the String.TrimStart(char[]). TrimStart(char[]) removes all mentioned chars from the start of a string until the first char which isn't mentioned.

if (listBox1.Items[i].ToString().Contains(empty))
{
    listBox1.SetSelected(i, true);
    var new = listBox1.SelectedItem.ToString();
// Assuming i is the new number.
    new =i.ToString() + &quot; &quot; + new.TrimStart(&#39; &#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;, &#39;0&#39;);
    listBox1.Items.Insert(listBox1.SelectedIndex, new);
    listBox1.Items.RemoveAt(listBox1.SelectedIndex);
    listBox1.ClearSelected();
 }

Source:
https://learn.microsoft.com/en-us/dotnet/api/system.string.trimstart?view=net-7.0

I also recommend not to use the "new" as a name for anything.

huangapple
  • 本文由 发表于 2023年5月22日 17:45:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定