I get a list of virtuals machines from my hypervisor and get this kind of string :
Vmid Name File Guest OS Version Annotation\n1 Win7-1 [VMs] Win7-1/Win7-1.vmx windows7_64Guest vmx-19 \n2 TestVM [VMs] TestVM/TestVM.vmx windows9_64Guest vmx-19 \n
Now I want to split this string by \n to get a string[] with :
Vmid Name File Guest OS Version Annotation
1 Win7-1 [VMs] Win7-1/Win7-1.vmx windows7_64Guest vmx-19
2 TestVM [VMs] TestVM/TestVM.vmx windows9_64Guest vmx-19
But I can't...
client.Connect();
var commandList = client.CreateCommand(@"vim-cmd vmsvc/getallvms");
commandList.Execute();
string[] resultatList = commandList.Result.Split(new string[] { @"\n" }, StringSplitOptions.None);
client.Disconnect();
This is the content of commandList.Result :
And the content of resultatList :
resultatList is an array with just 1 entry instead of 3...
What's wrong ?
CodePudding user response:
You are using @ so the string \n is litteraly a \ and a n. Your string have new lines not the 2 chars \ and n.
string[] resultatList = commandList.Result.Split(new string[] { "\n" }, StringSplitOptions.None);
Will split your string.
CodePudding user response:
Try removing the @
symbol because the '\n'
character you are seeing is a newline character.
string[] resultatList = commandList.Result.Split(new string[] { "\n" }, StringSplitOptions.None);
If you want cross-operating system compatability, you should split instead on Environment.NewLine
because in Windows systems the newline is two characters long '\r\n'
CodePudding user response:
\n is a character, not a string
Try this
commandList.Result.Split('\n')
and notice the '\n' is in single quotes.
Note that since your original string is ending in \n, you're going to get the last item as an empty string in the array.