gpt4 book ai didi

c# - 拆分 ;然后在 = 上,使用 String.Split 时遇到问题,需要正则表达式

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

"key1"="value1 http://www.example.com?a=1";"key2"="value2 http://www.example.com?a=2";

我需要将上面的行拆分 2 次,第一次是逗号;第二次在=号上。

它不能正常工作,因为值部分也有 = 符号。

我的代码不起作用,因为它假设值部分没有“=”符号,并且它没有简单地使用 String.Split('=') 的正则表达式。

有人可以帮助解决所需的正则表达式吗,我在键/值两边都添加了双引号以帮助将事物分开。

最佳答案

我没有使用正则表达式,但您可以执行以下操作:

        string test =@"""key1""=""value1 http://www.example.com?a=1"";""key2""=""value2 http://www.example.com?a=2""";

string[] arr = test.Split(';');

foreach (string s in arr)
{
int index = s.IndexOf('=');
string key = s.Substring(0, index);

string value = s.Substring(index+1, s.Length - index);
}

关于c# - 拆分 ;然后在 = 上,使用 String.Split 时遇到问题,需要正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13498610/

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