C# 如何删除列表中每个字符串上的空白?

C# 如何删除列表中每个字符串上的空白?,c#,listview,cmd,whitespace,C#,Listview,Cmd,Whitespace,我有一个列表,希望通过命令行运行ListViewItem中的每个项目。目前,在我看来,我的列表正在传播每个项目,在文本之前有一个空格,这会导致命令行无法将字符串识别为有效数据。如何删除列表中每个字符串值开头和结尾的空格 注意:您可以在标记为STAGE2的区域中查看列表的结构 private void wifiButton_Click(object sender, EventArgs e) { // STAGE 1 -- Query wifi profile

我有一个列表,希望通过命令行运行ListViewItem中的每个项目。目前,在我看来,我的列表正在传播每个项目,在文本之前有一个空格,这会导致命令行无法将字符串识别为有效数据。如何删除列表中每个字符串值开头和结尾的空格

注意:您可以在标记为STAGE2的区域中查看列表的结构

        private void wifiButton_Click(object sender, EventArgs e)
    {
        // STAGE 1 -- Query wifi profiles saved on device and isolate SSIDs
        Process cmd = new Process();
        cmd.StartInfo.FileName = "netsh.exe";
        System.Threading.Thread.Sleep(50);
        cmd.StartInfo.Arguments = "wlan show profiles";
        cmd.StartInfo.UseShellExecute = false;
        cmd.StartInfo.RedirectStandardOutput = true;
        cmd.StartInfo.RedirectStandardError = true;
        cmd.Start();
        //* Read the output (or the error)
        string output = cmd.StandardOutput.ReadToEnd();
        textBox3.Text = output;
        cmd.WaitForExit();
        System.Threading.Thread.Sleep(50);


        // output would be set by earlier code
        // STAGE 2 remove extra data in string down to the SSID name, then add insividual results into a list
        var regex = new Regex(@" User Profile\s+:(.*)");
        var resultList = new List<string>();

        foreach (Match match in regex.Matches(output))
        {
            output = string.Concat(output.Split(' '));
            resultList.Add(match.Groups[1].ToString());
            textBox4.Items.Add(match.Groups[1].ToString());
        }

        System.Threading.Thread.Sleep(500);
        // STAGE 3 For each item in the list created in stage 2, run individual SSID name through netsh and add results to textbox5. 
        foreach (ListViewItem item in textBox4.Items)
        {
            //arg = arg.Remove(0, 15);
            Process cmd2 = new Process();
            cmd2.StartInfo.FileName = "netsh.exe";
            cmd2.StartInfo.Arguments = "wlan show profiles name=" + item;  
            cmd2.StartInfo.UseShellExecute = false;
            cmd2.StartInfo.RedirectStandardOutput = true;
            cmd2.StartInfo.RedirectStandardError = true;
            cmd2.Start();
            //* Read the output (or the error)
            string output2 = cmd2.StandardOutput.ReadToEnd();
            textBox5.Text += output2;
            cmd2.WaitForExit();
        }
     }
private void wifiButton_单击(对象发送方,事件参数e)
{
//阶段1——查询设备上保存的wifi配置文件并隔离SSID
Process cmd=新流程();
cmd.StartInfo.FileName=“netsh.exe”;
系统.线程.线程.睡眠(50);
cmd.StartInfo.Arguments=“wlan显示配置文件”;
cmd.StartInfo.UseShellExecute=false;
cmd.StartInfo.RedirectStandardOutput=true;
cmd.StartInfo.RedirectStandardError=true;
cmd.Start();
//*读取输出(或错误)
字符串输出=cmd.StandardOutput.ReadToEnd();
textBox3.Text=输出;
cmd.WaitForExit();
系统.线程.线程.睡眠(50);
//输出将由前面的代码设置
//第2阶段删除字符串中直到SSID名称的额外数据,然后将单个结果添加到列表中
var regex=new regex(@“用户配置文件\s+:(*));
var resultList=新列表();
foreach(regex.Matches中的匹配(输出))
{
output=string.Concat(output.Split(“”));
结果列表.Add(match.Groups[1].ToString());
textBox4.Items.Add(match.Groups[1].ToString());
}
系统.线程.线程.睡眠(500);
//阶段3对于在阶段2中创建的列表中的每个项目,通过netsh运行单个SSID名称,并将结果添加到textbox5。
foreach(文本框4.Items中的ListViewItem项)
{
//arg=arg.Remove(0,15);
Process cmd2=新流程();
cmd2.StartInfo.FileName=“netsh.exe”;
cmd2.StartInfo.Arguments=“wlan显示配置文件名称=“+项;
cmd2.StartInfo.UseShellExecute=false;
cmd2.StartInfo.RedirectStandardOutput=true;
cmd2.StartInfo.RedirectStandardError=true;
cmd2.Start();
//*读取输出(或错误)
字符串output2=cmd2.StandardOutput.ReadToEnd();
textBox5.Text+=output2;
cmd2.WaitForExit();
}
}
使用
Trim()

在这一行

 cmd2.StartInfo.Arguments = "wlan show profiles name=" + item;  
您正在将ListViewItem连接到字符串。这是通过调用ListViewItem的ToString方法解决的,该方法生成如下内容

 cmd2.StartInfo.Arguments = "wlan show profiles name=ListViewItem: {...whatever...}
当然,这不是有效的
netsh
命令

你需要把它改成

 cmd2.StartInfo.Arguments = "wlan show profiles name=" + item.Text.Trim();  

(Trimmin,以防Regex有效地保留了一些空白)

@adamheg我在过去3天里一直在寻找。并且非常了解.Trim(),事实上,您可以看到我在代码中使用正则表达式。请不要只是来侮辱我。我只是在这里没有看到一些东西,而是问了一个关于我对C#的理解中的一个具体差距的具体问题。我不认为问题是从字符串中删除空格,但是将ListViewItem连接到字符串。请接受我的道歉。因此,我可以不写控制台日志而直接将这些注入到我的列表中吗?或者可能是第二份名单?还是我误解了预期的结果。老兄。。。。我不知道你能在这样一个占位符的末尾加上tring。你真是太棒了,感谢你在发表一些一般性评论/反驳问题之前通读了问题并理解了问题。
 cmd2.StartInfo.Arguments = "wlan show profiles name=" + item.Text.Trim();