将做工程过程比较好的内容做个记录,下边代码段是关于提供几个常用的C#字符串操作函数的代码。
public static string GetLeft(string str, string s)
{
string temp = str.Substring(0, str.IndexOf(s));
return temp;
}
public static string GetRight(string str, string s)
{
string temp = str.Substring(str.IndexOf(s), str.Length-str.Substring(0, str.IndexOf(s)).Length);
return temp;
}
public static string Between(string str, string leftstr, string rightstr)
{
int i = str.IndexOf(leftstr) + leftstr.Length;
string temp = str.Substring(i, str.IndexOf(rightstr, i) - i);
return temp;
}
public List<string> BetweenArr(string str, string leftstr, string rightstr)
{
List<string> list = new List<string>();
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;
}
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();
}
网友评论