【问题标题】:How can i delete whitespace on each string in my list?如何删除列表中每个字符串上的空格?
【发布时间】:2015-08-26 05:09:37
【问题描述】:

我有一个列表,想通过命令行运行 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();
        }
     }

【问题讨论】:

  • @AdamHeeg 我找了过去 3 天。并且非常了解 .Trim(),事实上你可以看到我在我的代码中使用了 Regex。请不要来这里只是为了侮辱我。我只是在这里没有看到任何东西,而是在问一个关于我对 C# 的理解存在特定差距的特定问题。
  • 我认为问题不是从字符串中删除空格,而是将 ListViewItem 连接到字符串。
  • 请接受我的道歉。

标签: c# listview cmd whitespace


【解决方案1】:

在这一行

 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();  

(修剪以防正则表达式有效地保留一些空格)

【讨论】:

  • Dude....我不知道你可以在这样的占位符末尾添加tring。您真是太棒了,感谢您在发布一些通用评论/提出问题之前通读问题并理解问题。
【解决方案2】:

使用Trim();

string stringToTrim = "   asdf    ";
Console.Log(stringToTrim.Trim());

// Output: 'asdf'

【讨论】:

  • 所以我可以简单地将这些直接注入到我的列表中,而不是写入控制台日志吗?或者可能是第二个列表?还是我误解了预期的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-10
  • 2022-01-06
  • 2011-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多