从字符串的键值对C#提取值 [英] c# extract values from key-value pairs in string

查看:894
本文介绍了从字符串的键值对C#提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串:

<$p$p><$c$c>blablablamorecontentblablabla?name=michel&score=5&age=28&iliedabouttheage=true

看起来像一个普通的查询字符串,是的,但我没有在任何网络环境

现在我想(=号后)提取值由他们的关键,例如,名称是什么(米歇尔),得分(5),年龄(28)等。

通常我解析字符串一样得到了这个词名称的字符串中的位置,再加入5到它(长的名字='),并命名这个位置开始,然后搜索和放大器; -sign并命名该位置'端',然后得到的位置开始和结束的字符串。

但是,必须有一个更好的解决方案,这是一个正则表达式的事情?

解决方案

尝试<一href="http://msdn.microsoft.com/en-us/library/ms150046.aspx">System.Web.HttpUtility.ParseQueryString,问号后面传递的一切。您需要使用System.Web程序集,但它不应该要求网络环境。

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.

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.

But there must be a better solution, is this a regex thing?

解决方案

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.

这篇关于从字符串的键值对C#提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆