Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在列表框中循环并删除某个字符之前的单词?_C#_For Loop_Listbox - Fatal编程技术网

C# 如何在列表框中循环并删除某个字符之前的单词?

C# 如何在列表框中循环并删除某个字符之前的单词?,c#,for-loop,listbox,C#,For Loop,Listbox,如何使用for循环遍历列表框并删除某个字符之前的单词 例如,如果我的列表框包含类似以下内容的项目: ','ae5e87df42fa5921 我想删除,“列表框中每个项目之前的所有内容,我该如何删除 谢谢 在for循环中,您只需调用item=item.Substring(item.LastIndexOf(“,””) 像这样: 正在运行。文本没有被替换:/@EdwinTorres那么你做错了什么。我已经编辑了我的帖子-这里有完整的示例,您可以创建一个新表单并用编辑过的表单替换它的代码。嘿,我将此项

如何使用for循环遍历列表框并删除某个字符之前的单词

例如,如果我的列表框包含类似以下内容的项目:

','ae5e87df42fa5921
我想删除
,“
列表框中每个项目之前的所有内容,我该如何删除


谢谢

在for循环中,您只需调用
item=item.Substring(item.LastIndexOf(“,””)
像这样:


正在运行。

文本没有被替换:/@EdwinTorres那么你做错了什么。我已经编辑了我的帖子-这里有完整的示例,您可以创建一个新表单并用编辑过的表单替换它的代码。嘿,我将此项添加到列表框:543225','11234134,并将代码添加到按钮。当我运行它时,项目现在是:','11234134而不是11234134我想要它的方式。
ListBox lb = new ListBox();
lb.Items.Add("12341','2341");
lb.Items.Add("123415','112341");
lb.Items.Add("543225','11234134");
for (int i = 0; i < lb.Items.Count; i++) {
    string item = lb.Items[i] as string;
    item = item.Substring(item.LastIndexOf("','"));
    lb.Items[i] = item;
}
public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        lb.Items.Add("12341','2341");
        lb.Items.Add("123415','112341");
        lb.Items.Add("543225','11234134");
    }

    private void button1_Click(object sender, EventArgs e) {
        for (int i = 0; i < lb.Items.Count; i++) {
            string item = lb.Items[i] as string;
            item = item.Substring(item.LastIndexOf("','"));
            lb.Items[i] = item;
        }
    }
}