简体   繁体   English

从分号分隔的字符串中提取值

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

I have a string from where I am trying to extract the value assigned to AccountName我有一个字符串,我试图从中提取分配给 AccountName 的值

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

So my desired output is "myAccountName".所以我想要的 output 是“myAccountName”。 Is RegEx the best way to go? RegEx 是 go 的最佳方式吗?

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.我想你可以把上面放在一行中,但是 regex.Split 看起来是最好的。

So, we basic chop the string in "two parts" based on AccountName=因此,我们根据 AccountName= 将字符串基本分为“两部分”

Then grab 2nd array value, and chop on ";", and we get our result.然后获取第二个数组值,并在“;”上打上标记,我们就得到了结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM