gpt4 book ai didi

c# - 如何使用字符串定界符拆分字符串?

转载 作者:行者123 更新时间:2023-11-30 13:50:37 25 4
gpt4 key购买 nike

如何使用字符串分隔符拆分字符串?

我试过:

string[] htmlItems = correctHtml.Split("<tr");

我得到错误:

Cannot convert from 'string' to 'char[]'

根据给定的字符串参数拆分字符串的推荐方法是什么?

最佳答案

有一个版本string.Split接受一个字符串数组和一个选项参数:

string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] {"[stop]"};
string[] result = source.Split(stringSeparators, StringSplitOptions.None);

所以即使你只有一个分隔符你想拆分你仍然必须将它作为一个数组传递。

以 Mike Hofer 的回答为起点,此扩展方法将使其使用起来更简单一些。

public static string[] Split(this string value, string separator)
{
return value.Split(new string[] {separator}, StringSplitOptions.None);
}

关于c# - 如何使用字符串定界符拆分字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5970666/

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