c#从字符串中的键值对中提取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2049048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# extract values from key-value pairs in string
提问by Michel
i have a string like this:
我有一个这样的字符串:
blablablamorecontentblablabla?name=michel&score=5&age=28&iliedabouttheage=true
looks like a regular query string yes, but i'm not in any web context
看起来像一个常规的查询字符串是的,但我不在任何网络上下文中
now i want to extract the values (after the = sign) by their key , for example,what is the name (michel), the score(5), the age(28) etc.
现在我想通过它们的键提取值(在 = 符号之后),例如,什么是名字(米歇尔),分数(5),年龄(28)等。
Normally i parse the string like get the position in the string of the word 'name', then add 5 to it (length of 'name=') and name this position 'start' then search for the &-sign and name that position 'end', and then get the string between the position start and end.
通常,我解析字符串,例如获取单词“name”在字符串中的位置,然后向其添加 5(“name=”的长度)并将此位置命名为“start”,然后搜索 & 符号并命名该位置'end',然后获取位置开始和结束之间的字符串。
But there must be a better solution, is this a regex thing?
但是必须有更好的解决方案,这是正则表达式吗?
采纳答案by Eric Petroelje
Try System.Web.HttpUtility.ParseQueryString, passing in everything after the question mark. You would need to use the System.Web assembly, but it shouldn't require a web context.
尝试System.Web.HttpUtility.ParseQueryString,在问号后传入所有内容。您将需要使用 System.Web 程序集,但它不应需要 Web 上下文。
回答by Otávio Décio
Not really, this can be done just with the Split function (what follows is kinda pseudo code, you need to add bound checks)
不是真的,这可以通过 Split 函数来完成(下面是伪代码,你需要添加边界检查)
string[] query = value.Split('?');
foreach (string pairs in query[1].Split('&')
{
string[] values = pairs.split('=');
}
Then iterate over the values variable and get the things you need.
然后迭代 values 变量并获得您需要的东西。
回答by CodeMonkey1313
As you suggested, I would recommend regex, probably a pattern like
正如你所建议的,我会推荐正则表达式,可能是一种模式
(?:.)*\?(.*\&.*)*
I know there's something else that can be used to cause the regex to ignore the first part [added, I think], but I can't think of it. That would get you a kvp, then you'd have to split the result on "&" (String.Split('&'))
我知道还有其他东西可以用来导致正则表达式忽略第一部分 [添加,我认为],但我想不出来。那会给你一个 kvp,然后你必须将结果拆分为 "&" (String.Split('&'))
回答by Wix
You could do a split on the '&' and then one on '='. You would have to deal the '?' at the beginning. Or if the string will always 'look' like a querystring then you could still treat it as such and try something like this class QueryString class useful for querystring manipulation, appendage, etc
您可以在“&”上进行拆分,然后在“=”上进行拆分。你将不得不处理“?” 一开始。或者,如果字符串总是“看起来”像一个查询字符串,那么你仍然可以这样对待它,并尝试像这样的类QueryString 类对查询字符串操作、附加等有用
回答by dcp
You can use the split method
您可以使用拆分方法
private static void DoIt()
{
string x = "blablablamorecontentblablabla?name=michel&score=5&age=28&iliedabouttheage=true";
string[] arr = x.Split("?".ToCharArray());
if (arr.Length >= 2)
{
string[] arr2 = arr[1].Split("&".ToCharArray());
foreach (string item in arr2)
{
string[] arr3 = item.Split("=".ToCharArray());
Console.WriteLine("key = " + arr3[0] + " value = " + arr3[1]);
}
}
}
Output:
输出:
key = name value = michel
key = score value = 5
key = age value = 28
key = iliedabouttheage value = true
回答by Lazarus
I'd probably go down the Split route.
我可能会沿着斯普利特路线走下去。
string input = "name=michel&score=5&age=28&iliedabouttheage=true";
string[] pairs = input.Split('&');
Dictionary<string,string> results = new Dictionary<string,string>();
foreach (string pair in pairs)
{
string[] paramvalue = pair.Split('=');
results.Add(paramvalue[0],paramvalue[1]);
}
回答by LukeH
If you want to create a dictionary of the key/value pairs then you could use a bit of LINQ:
如果要创建键/值对的字典,则可以使用一些 LINQ:
Dictionary<string, string> yourDictionary =
yourString.Split('?')[1]
.Split('&')
.Select(x => x.Split('='))
.ToDictionary(y => y[0], y => y[1]);
(You could skip the Split('?')[1]
part if your string contained just the querystring rather than the entire URL.)
(Split('?')[1]
如果您的字符串只包含查询字符串而不是整个 URL,您可以跳过该部分。)