Parse a semicolon separated list

I have a list of values โ€‹โ€‹separated by a semicolon, for example:

strins s = "param1=true;param2=4;param3=2.0f;param4=sometext;"; 

I need functions:

 public bool ExtractBool(string parameterName, string @params); public int ExtractInt(string parameterName, string @params); public float ExtractFloat(string parameterName, string @params); public string ExtractString(string parameterName, string @params); 

Are there any special features in .net that can help me with semicolon separated?

PS: parameter names are equal inside the list.

+6
c # algorithm csv
source share
3 answers

As a starting point, you will need the String.Split () method - it will split your string into an array of strings.

You will find many examples of this all over the Internet.

+3
source share

For a better line, you can use Split(',');

You can try this for comma separated

 string s ="param1=true;param2=4;param3=2.0f;param4=sometext;"; string[] sArray = s.Split(',') 
+1
source share

Here is the best way to do this and avoid having a whole bunch of empty array elements:

  string lsToString = "Your String Here"; string[] laChars = { "," }; string[] laTo = lsToString.Split(laChars, StringSplitOptions.RemoveEmptyEntries); 

This makes it easier to work after you split the line, because you do not need to worry about empty elements.

Pete

+1
source share

All Articles