C# Split 字符文本中的字符太多

时间:2023-12-04 12:06:20

问题:

string[] sArray = html.Split(new char[] { '<h1>', '</h1>' });

C# Split 字符文本中的字符太多

原因:

Split()里面是用char类型不是string字符串类型,所以只能用一个字符,必须先把多个字符替换成一个字符,然后再分割。

解决方案一:

string html = "<dd><h1> 第二百三十六章 古神精神印记</h1></dd>";
html = html.Replace("<h1>","*").Replace("</h1>","*");
string title = html.Split('*')[]; // 第二百三十六章 古神精神印记

解决方案二:

string title = html.Split(new string[] { "<h1>", "</h1>" }, StringSplitOptions.RemoveEmptyEntries)[];

相关文章:C#截取字符串的方法小结