'Extracting values from a semi-colon delimited string
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 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
