欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 取字符串中间文本 取字符串左边 取字符串右边

程序员文章站 2022-10-16 15:10:27
1 public static string Between(string str, string leftstr, string rightstr) //取文本中间 2 { 3 if (str != null && str.Length == 0) return ""; 4 if (leftstr... ......
 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         }