C# 文本键值对与Dictionary互转

C# 采用正则方式实现文本键值对与泛型字典Dictionary<string,string>相互转换

作者: 张赐荣

在开发过程中经常会遇到想将文本型字典Dictionary<string,string>转换为文本键值对进行保存,或者想要把已经保存的文本键值对还原回文本字典。
以下两个C#方法实现了文本字典转换为字符串文本及字符串文本键值对还原成字典的功能。
注:进行Base64编码是为了防止字符干扰正则匹配。

        public static string DictionaryToStringData (Dictionary<string,string> dict)  // 将文本型字典转换为键值对文本 {k=v} 格式,转换失败返回空文本
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in dict)
            {
                sb.Append("{\""+Convert.ToBase64String(Encoding.Default.GetBytes(item.Key))+"\"=\""+Convert.ToBase64String(Encoding.Default.GetBytes(item.Value))+"\"}\n");
            }
            return (sb.ToString());
        }

        public static Dictionary<string,string> StringDataToDictionary (string data)  // 将文本键值对 {k=v} 转换为文本型字典,转换失败返回 null
        {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("\\{\"(.+?)\"=\"(.+?)\"\\}",    System.Text.RegularExpressions.RegexOptions.IgnoreCase|System.Text.RegularExpressions.RegexOptions.Multiline);
            System.Text.RegularExpressions.MatchCollection mc = regex.Matches(data);
            if (mc.Count < 1)
            {
                return (null);
            }
            Dictionary<string, string> kv = new Dictionary<string, string>();
            foreach (System.Text.RegularExpressions.Match item in mc)
            {
                kv.Add(Encoding.Default.GetString(Convert.FromBase64String(item.Groups[1].Value)),Encoding.Default.GetString(Convert.FromBase64String(item.Groups[2].Value)));
            }
            return (kv);
        }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用异步方法来处理键值对。具体实现可以使用异步的Dictionary类,它提供了一些异步的方法来访问和操作键值对。例如,可以使用异步的AddAsync方法来向字典中添加键值对,使用异步的ContainsKeyAsync方法来检查是否存在某个键等等。以下是一个使用异步方法处理键值对的示例代码: ```csharp using System.Collections.Generic; using System.Threading.Tasks; class Program { static Dictionary<string, string> dictionary = new Dictionary<string, string>(); static async Task Main(string[] args) { await dictionary.AddAsync("key1", "value1"); bool containsKey = await dictionary.ContainsKeyAsync("key1"); string value = await dictionary.GetValueAsync("key1"); await dictionary.RemoveAsync("key1"); } } public static class DictionaryExtensions { public static async Task AddAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value) { await Task.Run(() => dictionary.Add(key, value)); } public static async Task<bool> ContainsKeyAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) { return await Task.Run(() => dictionary.ContainsKey(key)); } public static async Task<TValue> GetValueAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) { return await Task.Run(() => dictionary[key]); } public static async Task RemoveAsync<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) { await Task.Run(() => dictionary.Remove(key)); } } ``` 在这个示例中,我们使用了异步方法AddAsync、ContainsKeyAsync、GetValueAsync和RemoveAsync来向字典中添加、检查、获取和删除键值对。这些方法都是异步的,可以在异步环境下使用,比如在异步方法或线程池任务中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值