Skip to main content
The 2024 Developer Survey results are live! See the results
edited tags
Link
abatishchev
  • 99.6k
  • 88
  • 300
  • 439
(its = possessive, it's = "it is" or "it has".)
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132

what What is the best way to parse this string in C#?

iI have a string that iI am reading from another system. itsIt's basically a long string that represents a list of key value pairs that are separated by a space in between. ItIt looks like this:

 key:value[space]key:value[space]key:value[space]

so iSo I wrote this code to parse it:

string myString = ReadinString();
string[] tokens = myString.split(' ');
foreach (string token in tokens) {
     string key = token.split(':')[0];
     string value = token.split(':')[1];
     .  . . . 
}

theThe issue now is that some of the values have spaces in them so my "simplistic" split at the top no longer works. iI wanted to see how iI could still parse out the list of key value pairs (given space as a separator character) now that iI know there also could be spaces in the value field as split doesn't seem like itsit's going to be able to work anymore.

NOTE: I now confirmed that KEYs will NOT have spaces in them so iI only have to worry about Valuesthe values. Apologies for the confusion.

what is the best way to parse this string in C#

i have a string that i am reading from another system. its basically a long string that represents a list of key value pairs that are separated by a space in between. It looks like this:

 key:value[space]key:value[space]key:value[space]

so i wrote this code to parse:

string myString = ReadinString();
string[] tokens = myString.split(' ');
foreach (string token in tokens) {
     string key = token.split(':')[0];
     string value = token.split(':')[1];
     .  . . . 
}

the issue now is that some of the values have spaces in them so my "simplistic" split at the top no longer works. i wanted to see how i could still parse out the list of key value pairs (given space as a separator character) now that i know there also could be spaces in the value field as split doesn't seem like its going to be able to work anymore

NOTE: I now confirmed that KEYs will NOT have spaces in them so i only have to worry about Values. Apologies for the confusion.

What is the best way to parse this string in C#?

I have a string that I am reading from another system. It's basically a long string that represents a list of key value pairs that are separated by a space in between. It looks like this:

 key:value[space]key:value[space]key:value[space]

So I wrote this code to parse it:

string myString = ReadinString();
string[] tokens = myString.split(' ');
foreach (string token in tokens) {
     string key = token.split(':')[0];
     string value = token.split(':')[1];
     .  . . . 
}

The issue now is that some of the values have spaces in them so my "simplistic" split at the top no longer works. I wanted to see how I could still parse out the list of key value pairs (given space as a separator character) now that I know there also could be spaces in the value field as split doesn't seem like it's going to be able to work anymore.

NOTE: I now confirmed that KEYs will NOT have spaces in them so I only have to worry about the values. Apologies for the confusion.

added 41 characters in body
Source Link
leora
  • 194.2k
  • 366
  • 897
  • 1.4k

i have a string that i am reading from another system. its basically a long string that represents a list of key value pairs that are separated by a space in between. It looks like this:

 key:value[space]key:value[space]key:value[space]

so i wrote this code to parse:

string myString = ReadinString();
string[] tokens = myString.split(' ');
foreach (string token in tokens) {
     string key = token.split(':')[0];
     string value = token.split(':')[1];
     .  . . . 
}

the issue now is that some of the values have spaces in them so my "simplistic" split at the top no longer works. i wanted to see how i could still parse out the list of key value pairs (given space as a separator character) now that i know there also could be spaces in the value field as split doesn't seem like its going to be able to work anymore

NOTE: I now confirmed that KEYs will NOT have spaces in them. Apologies for the confusion.NOTE: I now confirmed that KEYs will NOT have spaces in them so i only have to worry about Values. Apologies for the confusion.

i have a string that i am reading from another system. its basically a long string that represents a list of key value pairs that are separated by a space in between. It looks like this:

 key:value[space]key:value[space]key:value[space]

so i wrote this code to parse:

string myString = ReadinString();
string[] tokens = myString.split(' ');
foreach (string token in tokens) {
     string key = token.split(':')[0];
     string value = token.split(':')[1];
     .  . . . 
}

the issue now is that some of the values have spaces in them so my "simplistic" split at the top no longer works. i wanted to see how i could still parse out the list of key value pairs (given space as a separator character) now that i know there also could be spaces in the value field as split doesn't seem like its going to be able to work anymore

NOTE: I now confirmed that KEYs will NOT have spaces in them. Apologies for the confusion.

i have a string that i am reading from another system. its basically a long string that represents a list of key value pairs that are separated by a space in between. It looks like this:

 key:value[space]key:value[space]key:value[space]

so i wrote this code to parse:

string myString = ReadinString();
string[] tokens = myString.split(' ');
foreach (string token in tokens) {
     string key = token.split(':')[0];
     string value = token.split(':')[1];
     .  . . . 
}

the issue now is that some of the values have spaces in them so my "simplistic" split at the top no longer works. i wanted to see how i could still parse out the list of key value pairs (given space as a separator character) now that i know there also could be spaces in the value field as split doesn't seem like its going to be able to work anymore

NOTE: I now confirmed that KEYs will NOT have spaces in them so i only have to worry about Values. Apologies for the confusion.

added 73 characters in body; added 1 characters in body
Source Link
leora
  • 194.2k
  • 366
  • 897
  • 1.4k
Loading
deleted 33 characters in body
Source Link
leora
  • 194.2k
  • 366
  • 897
  • 1.4k
Loading
Source Link
leora
  • 194.2k
  • 366
  • 897
  • 1.4k
Loading