static bool CheckLable(List <LableInfo> list, ref int index) { LableInfo tmp = list[index]; index++; if (list[index].Type == 0) { if (!CheckLable(list, ref index)) { return(false); } if (index >= list.Count) { return(false); } } int c = list[index].Lable; index++; if (c == tmp.Lable) { return(true); } return(false); }
/// <summary> /// 删除富文本中的标签 /// </summary> /// <param name="str">富文本</param> /// <returns></returns> public static string DeleteLabel(string str) { if (tmp == null) { tmp = new StringBuilder(); lables = new List <LableInfo>(); } else { tmp.Clear(); lables.Clear(); } int index = 0; LableInfo lable = new LableInfo(); for (int i = 0; i < str.Length; i++) { if (GetLable(str, index, ref lable)) { index = lable.Start + lable.Length; lables.Add(lable); } else { break; } } int c = lables.Count; if (c % 2 > 0)//标签不是成对的 { return(str); } index = 0; for (int i = 0; i < lables.Count; i++) { if (index >= lables.Count) { break; } if (!CheckLable(lables, ref index)) { return(str); } } if (lables.Count == 0) { return(str); } int s = 0; for (int i = 0; i < lables.Count; i++) { int a = lables[i].Start; int l = lables[i].Length; int len = a - s; if (len > 0) { tmp.Append(str, s, len); } s = a + l; } int ol = str.Length - s; if (ol > 0) { tmp.Append(str, s, ol); } return(tmp.ToString()); }
static bool GetLable(string str, int index, ref LableInfo info) { int s = str.IndexOf(Start, index); if (s >= 0) { info.Start = s; info.Length = 1; int ss = s + 1; if (ss < str.Length) { if (str[ss] == '/')//这是标签的结尾 { for (int i = 0; i < LableEnd.Length; i++) { if (Compare(str, s, LableEnd[i])) { info.Start = s; info.Length = LableEnd[i].Length; info.Type = 1; info.Lable = i; return(true); } } } else { for (int i = 0; i < LableStart.Length; i++) { if (Compare(str, s, LableStart[i])) { int next = LableStart[i].Length; char c = str[s + next]; info.Start = s; info.Type = 0; info.Lable = i; if (c == equals) { if (i < 2) { info.Length = LableStart[i].Length + 1; } else { int e = str.IndexOf(end, s + LableStart[i].Length); if (e >= 0) { info.Length = e - s + 1; return(true); } else { return(false); } } } else if (c == end) { info.Length = LableStart[i].Length + 1; } return(true); } } } } } return(false); }