如何解析字符串以在其中找到键值对 [英] How to parse a string to find key-value pairs in it

查看:62
本文介绍了如何解析字符串以在其中找到键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google中搜索邮件时,我们使用

While searching mail in Google, we use the sytax like

from:devcoder hasattachments:true mySearchString on:11-aug

mySearchString from:devcoder on:11-aug anotherSearchKeyword

解析后,我应该得到键值对,例如(from devcoder),(on,11-aug). 在c#中实现此解析的最佳方法是什么.

After parsing, I should get the keyvalue pair such as (from, devcoder), (on, 11-aug). What is the best way to implement this parsing in c#.

推荐答案

要Linq-ify杰森的答案:

To Linq-ify Jason's answer:

string s = "from:devcoder hasattachments:true mySearchString on:11-aug";

var keyValuePairs = s.Split(' ')
    .Select(x => x.Split(':'))
    .Where(x => x.Length == 2)
    .ToDictionary(x => x.First(), x => x.Last());

这篇关于如何解析字符串以在其中找到键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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