Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a semicolon-separated list

I have a semicolon-separated list of values, for example:

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

I need a 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);

Is there a special functions in .net that can help me with semicolon-separated list ?

PS: parameter names are equal within a list.

like image 783
Александр Д. Avatar asked May 13 '10 07:05

Александр Д.


3 Answers

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

You'll find loads of examples of this all over the web.

like image 117
philiphobgen Avatar answered Oct 17 '22 01:10

philiphobgen


For string better you can use Split(';');

You can try this for split comma

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

string[] sArray = s.Split(';')
like image 45
anishMarokey Avatar answered Oct 17 '22 00:10

anishMarokey


Here is a better 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);

Makes it much easier to work with after you split the string because you don't have to worry about empty elements.

Pete

like image 33
DigiOz Multimedia Avatar answered Oct 17 '22 01:10

DigiOz Multimedia