0

I am trying to split a string in c#. I am receiving a string with multiple whitespaces and i want to split it at the whitspaces. So far all I have tried failed. It is not throwing an error put the string simply stays empty

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;
              }
          }
      }
}

I tried all of the methods I could find yet the result is the same. The string is there but the string array is empty.

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

Am I missing out something?

EDIT Sorry the error was on my Site. Debug does not output the Array. I have mistaken it to be empty.

Silve2611
  • 2,198
  • 2
  • 34
  • 55
  • 2
    `String.Split` works. It's used millions of times per day. Post the *relevant* code that demonstrates the problem, with examples. For all anyone knows your string may *not* contain the delimiters you think it does – Panagiotis Kanavos May 16 '18 at 09:34
  • What did you try, what were the inputs and what were the results?? BTW why are you trying to split with *no* characters? That makes no sense – Panagiotis Kanavos May 16 '18 at 09:35
  • 2
    what values is in `(string)mo["CommandLine"];`, please provide us – Prasad Telkikar May 16 '18 at 09:37
  • 1
    `Debug.WriteLine()` should show you `System.String[]`. Do click on [mcve]. You can rewrite this question without any menton of WMI or Process. – bommelding May 16 '18 at 09:38
  • 1
    The code above works fine, you're just outputting the wrong thing here. – DavidG May 16 '18 at 09:39
  • 1
    `Split` never returns an empty array, even if the separator doesn't exist in the string provided, the result will contain single item which is the argument itself. – Uladzislaŭ May 16 '18 at 09:42
  • @KeithNicholas That is not a good dupe target. The question isn't about the right type of whitespace, it's about not understanding C# – DavidG May 16 '18 at 09:46
  • @DavidG: So what is the right duplicate for that? :D – Tim Schmelter May 16 '18 at 09:49
  • @TimSchmelter Not sure, perhaps a C# tutorial? ;) – DavidG May 16 '18 at 09:52
  • Can you print the ascii code of each of the characters in that string to make sure they are indeed regular whitespaces? – Ricardo Alves May 16 '18 at 09:53
  • As you all pointed out very clearly the error is on my site, sorry. I was not aware that Debug.WriteLine was not able to output an array. How do I debug an Array? – Silve2611 May 16 '18 at 11:49
  • 1
    @Silve2611 You should probably delete this question now as you were working on a false assumption. – DavidG May 16 '18 at 13:26
  • i can't. i tried but it is not allowed because there was too many comments – Silve2611 May 16 '18 at 21:51

1 Answers1

0

String.Split() (no parameters) does split on all whitespace (including LF/CR)

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
Amc
  • 88
  • 7