gpt4 book ai didi

c# - 函数之间的字符串?

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

有没有办法在..让我们说引号之间获取字符串“使用 Indexof 和 substring 的问题是它得到第一个“和最后一个”而不是这对。喜欢

"Hello" "WHY ARE" "WWWWWEEEEEE"

会得到

Hello" "WHY ARE" "WWWWWEEEEEE

我希望它进入数组 > 你好,为什么,WWWWEEEEEE

有什么办法吗?

最佳答案

是这样的吗?

StringCollection resultList = new StringCollection();
try
{
Regex regexObj = new Regex("\"([^\"]+)\"");
Match matchResult = regexObj.Match(subjectString);

while (matchResult.Success)
{
resultList.Add(matchResult.Groups[1].Value);
matchResult = matchResult.NextMatch();
}
}
catch (ArgumentException ex)
{
// Syntax error in the regular expression
}

如果 subjectString"Hello""WHY ARE""WWWWWEEEEEE",那应该给你一个包含以下内容的列表:

  • 你好
  • 为什么
  • WWWWWEEEEEE

一个更紧凑的示例,它使用静态 Regex 类,并且只将匹配写入控制台而不是添加到集合中:

var subject = "\"Hello\" \"WHY ARE\" \"WWWWWEEEEEE\"";
var match = Regex.Match(subject, "\"([^\"]+)\"");

while (match.Success)
{
Console.WriteLine(match.Groups[1].Value);
match = match.NextMatch();
}

关于c# - 函数之间的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1321727/

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