gpt4 book ai didi

c# - 使用任何或所有分隔符之一按单词拆分字符串?

转载 作者:太空狗 更新时间:2023-10-29 22:20:29 24 4
gpt4 key购买 nike

我可能刚刚达到了我想得太多的地步,但我想知道:有没有办法指定一个特殊字符列表,这些字符都应该被视为定界符,然后使用该列表拆分字符串?示例:

"battlestar.galactica-season 1"

应返回为

battlestar galactica season 1

我在考虑正则表达式,但我现在有点慌张,盯着它看了太久。

编辑:感谢大家证实我的怀疑是我想多了哈哈:这就是我最终得到的结果:

//remove the delimiter
string[] tempString = fileTitle.Split(@"\/.-<>".ToCharArray());
fileTitle = "";
foreach (string part in tempString)
{
fileTitle += part + " ";
}

return fileTitle;

我想我也可以用“”空格替换定界符……我会在计时器一到就选择一个答案!

最佳答案

内置 String.Split方法可以将字符集合作为分隔符。

string s = "battlestar.galactica-season 1";
string[] words = s.split('.', '-');

关于c# - 使用任何或所有分隔符之一按单词拆分字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5983549/

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