- 1 public static string Between(string str, string leftstr, string rightstr) //取文本中间
- 2 {
- 3 if (str != null && str.Length == 0) return "";
- 4 if (leftstr != "")
- 5 {
- 6 int i = str.IndexOf(leftstr);//去除左边字符串之后第一个字符的位置
- 7 i = i + leftstr.Length;//左边字符串长度
- 8 if (rightstr != "")
- 9 {
- 10 int ResultLength = str.IndexOf(rightstr, i) - i;//取中间字符串长度
- 11 return str.Substring(i, ResultLength);
- 12 }
- 13 else return str.Substring(i, str.Length - i);//取字符串右边
- 14 }
- 15 else//取字符串左边
- 16 {
- 17 int i = str.IndexOf(rightstr);
- 18 if (i == 0) return "";
- 19 else return str.Substring(0, i);
- 20 }
- 21 }