[SOLVED] Extracting values from a semi-colon delimited string

Issue

I have a string from where I am trying to extract the value assigned to AccountName

var myString = "DefaultEndpointsProtocol=https;AccountName=myAccountName;AccountKey=myAccountKey;EndpointSuffix=whatever.com"

So my desired output is "myAccountName". Is RegEx the best way to go?

Solution

Hum, to split out that value?

This works:

        var myString = "DefaultEndpointsProtocol=https;AccountName=myAccountName;AccountKey=myAccountKey;EndpointSuffix=whatever.com";

        string[] MyDeLim = {"AccountName="};
        string res = myString.Split(MyDeLim ,StringSplitOptions.None)[1];
        res = res.Split(';')[0];
        Debug.Print(res);

However, regex, it even better, say like this:

        // however, this looks to be even better:
        // using System.Text.RegularExpressions;

        res = Regex.Split(myString,"AccountName=")[1];
        res = Regex.Split(res, ";")[0];

And you could I suppose put above into one line, but regex.Split looks to be about the best.

So, we basic chop the string in "two parts" based on AccountName=

Then grab 2nd array value, and chop on ";", and we get our result.

Answered By – Albert D. Kallal

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *