C# 如何使用细绳纤度计拆分细绳?

C# 如何使用细绳纤度计拆分细绳?,c#,string,.net-4.0,split,C#,String,.net 4.0,Split,如何使用细绳纤度计拆分细绳 我试过: string[] htmlItems = correctHtml.Split("<tr"); 根据给定的字符串参数拆分字符串的建议方法是什么?有一个版本采用字符串数组和选项参数: string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]"; string[] stringSeparators = new string[] {"[stop]"}; strin

如何使用细绳纤度计拆分细绳

我试过:

string[] htmlItems = correctHtml.Split("<tr");
根据给定的字符串参数拆分字符串的建议方法是什么?

有一个版本采用字符串数组和选项参数:

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);
}

看看Regex.Split


您还需要在拆分中使用StringSplitOptions参数。

这不是您正在搜索的重载吗?
编写扩展方法:

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

问题已解决。

我没有看到任何将字符串作为唯一参数的重载。我错过什么了吗@问题是如何在给定的单词上拆分字符串。谢谢,这非常有效,尽管代码有点复杂。:)好主意,不过这个实现会失败,因为分隔符比单个字符长。
public static string[] Split(this string value, string separator)
{
    return value.Split(separator.ToCharArray());
}