gpt4 book ai didi

c# - 从字符串中提取值

转载 作者:太空宇宙 更新时间:2023-11-03 17:54:04 25 4
gpt4 key购买 nike

从此string读取结果的最佳方法是什么?仅字符串需要解析,它是从Web服务返回的

"RESULT: FAILED
RESULT_CODE: 944
RRN: 313434415392
APPROVAL_CODE: 244447
CARD_NUMBER: 4***********3412";


我可以使用 mystring.split(' '),但是我认为这不是一个好主意。

最佳答案

您可以使用LINQ来构建字典:

string s = 
@"RESULT: FAILED
RESULT_CODE: 944
RRN: 313434415392
APPROVAL_CODE: 244447
CARD_NUMBER: 4***********3412";

IDictionary<string, string> result = s
.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Select(line => line.Split(':'))
.ToDictionary(x => x[0].Trim(), x => x[1].Trim());


然后按键查询结果:

Console.WriteLine(result["RRN"]);


会给你 313434415392

或者,如果您想获取所有键和值,只需循环:

foreach (var item in result)
{
Console.WriteLine("key: {0}, value: {1}", item.Key, item.Value);
}

关于c# - 从字符串中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16505202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com