1.从index截取到字符串结束:
string.Substring(index)
2.从index截取length长度的字符串:
string.Substring(index,length)
3.查找字符串最先出现的index:
//如果没找到,会返回-1
int index =string.IndexOf(s)
4.用字符串截取字符串:
int index =string.IndexOf(s);
string.Substring(index,index) // 左边
string temp = str.Substring(index, string.Length-string.Substring(0, str.IndexOf(s)).Length); // 右边
5.从中间取
int i = str.IndexOf(leftstr) + leftstr.Length;
string temp = str.Substring(i, str.IndexOf(rightstr, i) - i);
6.取文本中间到List集合
public static string Between(string str, string leftstr, string rightstr)
List<string> list = new List<string>();
int leftIndex = str.IndexOf(leftstr);//左文本起始位置
int leftlength = leftstr.Length;//左文本长度
int rightIndex = 0;
string temp = "";
while (leftIndex != -1) { rightIndex = str.IndexOf(rightstr, leftIndex + leftlength); if (rightIndex == -1) { break; } temp = str.Substring(leftIndex + leftlength, rightIndex - leftIndex - leftlength); list.Add(temp); leftIndex = str.IndexOf(leftstr, rightIndex + 1); } return list;
}
7.倒序
public static string StrReverse(string str)
{
char[] chars = str.ToCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < chars.Length; i++)
{
sb.Append(chars[chars.Length - 1 - i]);
}
return sb.ToString();
}
标签:index,常用,string,c#,Substring,IndexOf,int,str,文本 From: https://www.cnblogs.com/simpleyue/p/16727082.html