gpt4 book ai didi

C# 按 String[] 分隔符拆分,仅匹配精确单词

转载 作者:行者123 更新时间:2023-12-05 01:09:40 26 4
gpt4 key购买 nike

我有字符串,它的值应该由字符串分隔符分割。例如,我的分隔符是“at”,我只想按精确的“at”关键字分割字符串值。下面是我的示例字符串值

 var sampleStr= "at Metadata at quota at what at batter";
如果我使用下面的代码,其中带有“at”的单词也会被拆分。
var result= sampleStr.Split(new string[] { "at" }, StringSplitOptions.None);
我想要的结果是一个数组,如果组合起来将是“元数据配额什么击球手”。
请帮忙。

最佳答案

也许:

IEnumerable<string> wordsWithoutAt = sampleStr.Split()
.Where(w => !StringComparer.OrdinalIgnoreCase.Equals(w, "at"));
string result = string.Join(" " , wordsWithoutAt);

Demo

如果情况重要,请更换 StringComparer.OrdinalIgnoreCase部分与 != "at" .

关于C# 按 String[] 分隔符拆分,仅匹配精确单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15520089/

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