无法在c#中拆分字符串

时间:2018-05-16 09:32:02

标签: c# string windows visual-studio-2017

我正在尝试在c#中拆分字符串。我收到一个包含多个空格的字符串,我想将其拆分为空格。到目前为止,我所有尝试都失败了。它不会抛出错误,字符串只是保持空白

using System;
using System.Diagnostics;
using System.Management;

[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out int ProcessId);

public static Process GetActiveWindowProcess(){
    var processWithFocus = GetForegroundWindow();
    int processId;
    GetWindowThreadProcessId(processWithFocus, out processId);
    Process process = Process.GetProcessById(processId);
    return process;
}
public static String getCommandLine(){
      string wmiQueryString = "SELECT * FROM Win32_Process WHERE ProcessId = " + process.Id;
      using (var searcher = new ManagementObjectSearcher(wmiQueryString))
      {
          using (var results = searcher.Get())
          {
              ManagementObject mo = results.Cast<ManagementObject>().FirstOrDefault();
              if (mo != null)
              {
                  String str = (string)mo["CommandLine"];
                  Debug.WriteLine(str.Split(' ')); //Splitting the string
                  return str;
              }
          }
      }
}

我尝试了所有可以找到的方法但结果是一样的。字符串在那里,但字符串数组为空。

var ssizes = myStr.Split(" \t".ToCharArray());
string[] ssize = myStr.Split(null);
string[] ssize = myStr.Split(new char[0]);
//all arrays are emtpy...

我错过了什么吗?

修改 抱歉,错误发生在我的网站上。 Debug不输出Array。我误以为它是空的。

1 个答案:

答案 0 :(得分:0)

String.Split()(无参数)会在所有空格(包括LF / CR)上进行拆分