Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to split a string by "\n"

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :
commandlist content

And the content of resultatList :
resultatlist content

resultatList is an array with just 1 entry instead of 3…

What’s wrong ?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading