从字符串的键值对C#提取值字符串、键值

2023-09-04 02:06:47 作者:温柔养猫人

我有一个这样的字符串:

blablablamorecontentblablabla?name=michel&score=5&age=28&iliedabouttheage=true

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

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

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

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

解决方案

尝试System.Web.HttpUtility.ParseQueryString,问号后面传递的一切。您需要使用System.Web程序集,但它不应该要求网络环境。

i have a string like this:

blablablamorecontentblablabla?name=michel&score=5&age=28&iliedabouttheage=true
C 改注册表的键值 值的类型怎么会变

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.