C# 无法在c中拆分字符串#
我正在尝试用c#拆分一个字符串。我收到一个包含多个空格的字符串,我想在空格处拆分它。到目前为止,我所有的尝试都失败了。它不会抛出一个错误,因为字符串只是保持为空C# 无法在c中拆分字符串#,c#,string,windows,visual-studio-2017,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
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;
}
}
}
}
我遗漏了什么吗
编辑
抱歉,我的网站上出现了错误。调试不输出数组。我错认为是空的
String.Split()
(无参数)不会在所有空格(包括LF/CR)上进行分割。String.Split
有效。它每天使用数百万次。发布演示问题的相关代码,并附上示例。所有人都知道,您的字符串可能不包含您认为它不包含的分隔符,(string)mo[“CommandLine”]中的值是什么代码>,请向我们提供Debug.WriteLine()
应显示System.String[]
。一定要点击。你可以重写这个问题,而不需要任何WMI或进程。上面的代码工作正常,你只是在这里输出了错误的内容。Split
永远不会返回空数组,即使所提供的字符串中不存在分隔符,结果也将包含单个项,即参数本身。好吧,他说,如果你看最后一段代码,他也用了其他方法是的,即使你说的是正确的,恐怕这不是问题的答案。正如我指出的,错误是我错误的调试。谢谢你的帮助!
var ssizes = myStr.Split(" \t".ToCharArray());
string[] ssize = myStr.Split(null);
string[] ssize = myStr.Split(new char[0]);
//all arrays are emtpy...