按子字符串分割的函数

本文关键字:函数 分割 字符串 | 更新日期: 2023-09-27 18:14:39

我有文字

Hi, my name is <b>Dan</b> and i need/n to separate string

我需要的是找到特定的标签和单独的文本预定义的标签,如/n或(b),结果需要是:

Str[0] = Hi, my name is
Str[1] = Dan
Str[2] = and i need
Str[3] = to separate string

你能帮帮我吗?

按子字符串分割的函数

试试这个:

string[] separators = {"<b>", "</b>", "''n"};
string value = "Hi, my name is <b>Dan</b> and i need ''n to separate string";
string[] words = value.Split(separators, StringSplitOptions.RemoveEmptyEntries);

这应该能奏效:

const string source = "hi, my name is <b>Dan</b> and i need/n to separate string";
var res = Regex.Split(source, "(</?b>)|(/n)").Where(x => !Regex.IsMatch(x, "(</?b>)|(/n)")).ToArray();