Home > OS >  Extracting values from a semi-colon delimited string
Extracting values from a semi-colon delimited string

Time:03-24

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?

CodePudding user response:

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.

  • Related