这几天要用到一些字符串的操作,网上也没有找好好用的,于是只有自己写。
C#自带的字符串截取Substring只能从头或者指定位置
而IndexOf又只能得到字符串第一次出现的位置
写了三个功能更强一些的方法,本代码是基本C# 3.0的,应用在2.0中,将this声明去掉即可。
代码如下:
/// <summary>
/// 返回字符串中指字子字符串,指定出现位置指定长度的字符串
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="searchstr">要搜索的字符串</param>
/// <param name="lengthnum">返回字符串的长度,0或-1返回到尾部所有字符串</param>
/// <param name="startnum">字符串出现的位置</param>
/// <returns></returns>
public static string Substr(this string str, string searchstr,int lengthnum, int startnum)
{
string result = "";
string resultstr = "";
if (startnum < 1)
startnum = 1;
int searchstrlength = searchstr.Length; //要查找字符串的长度
if (startnum <= 1 && lengthnum >0)
result = str.Substring(0, lengthnum);
else if (startnum <= 1)
result = str.Substring(0);
else
{
int i = 1;
while (i<=startnum)
{
str = str.Substring(str.IndexOf(searchstr) + searchstrlength);
if (i == startnum)
{
if (lengthnum > 0)
result = str.Substring(0, lengthnum);
else
result = str;
break;
}
i++;
}
}
return result;
}
/// <summary>
/// 返回字符串中指字子字符串,指定出现次数的位置索引
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="searchstr">要搜索的字符串</param>
/// <param name="startnum">字符串出现的位置</param>
/// <returns></returns>
public static int IndexOfByNum(this string str, string searchstr, int startnum)
{
if (startnum < 1)
startnum = 1;
if (str.IndexOf(searchstr) == -1)
return -1;
else if(CountNum(str,searchstr) < startnum)
return -1;
int searchstrlength = searchstr.Length; //要查找字符串的长度
if (startnum == 1)
return str.IndexOf(searchstr);
else
{
return str.Substring(0,str.Length-str.Substr(searchstr,0,startnum).Length).Length;
}
}
/// <summary>
/// 统计字符串出现的次数
/// </summary>
/// <param name="text1">原字符串</param>
/// <param name="text2">要查找的字符串</param>
/// <returns>出现的次数</returns>
public static int CountNum(this string text1, string text2)
{
int total = 0;
int loact = text1.IndexOf(text2);
while (loact != -1)
{
int loc = text1.IndexOf(text2) + text2.Length;
int len = text1.Length - loc;
if (loc != -1)
{
text1 = text1.Substring(loc, len);
}
loact = text1.IndexOf(text2);
total++;
}
return total;
}
/// 返回字符串中指字子字符串,指定出现位置指定长度的字符串
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="searchstr">要搜索的字符串</param>
/// <param name="lengthnum">返回字符串的长度,0或-1返回到尾部所有字符串</param>
/// <param name="startnum">字符串出现的位置</param>
/// <returns></returns>
public static string Substr(this string str, string searchstr,int lengthnum, int startnum)
{
string result = "";
string resultstr = "";
if (startnum < 1)
startnum = 1;
int searchstrlength = searchstr.Length; //要查找字符串的长度
if (startnum <= 1 && lengthnum >0)
result = str.Substring(0, lengthnum);
else if (startnum <= 1)
result = str.Substring(0);
else
{
int i = 1;
while (i<=startnum)
{
str = str.Substring(str.IndexOf(searchstr) + searchstrlength);
if (i == startnum)
{
if (lengthnum > 0)
result = str.Substring(0, lengthnum);
else
result = str;
break;
}
i++;
}
}
return result;
}
/// <summary>
/// 返回字符串中指字子字符串,指定出现次数的位置索引
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="searchstr">要搜索的字符串</param>
/// <param name="startnum">字符串出现的位置</param>
/// <returns></returns>
public static int IndexOfByNum(this string str, string searchstr, int startnum)
{
if (startnum < 1)
startnum = 1;
if (str.IndexOf(searchstr) == -1)
return -1;
else if(CountNum(str,searchstr) < startnum)
return -1;
int searchstrlength = searchstr.Length; //要查找字符串的长度
if (startnum == 1)
return str.IndexOf(searchstr);
else
{
return str.Substring(0,str.Length-str.Substr(searchstr,0,startnum).Length).Length;
}
}
/// <summary>
/// 统计字符串出现的次数
/// </summary>
/// <param name="text1">原字符串</param>
/// <param name="text2">要查找的字符串</param>
/// <returns>出现的次数</returns>
public static int CountNum(this string text1, string text2)
{
int total = 0;
int loact = text1.IndexOf(text2);
while (loact != -1)
{
int loc = text1.IndexOf(text2) + text2.Length;
int len = text1.Length - loc;
if (loc != -1)
{
text1 = text1.Substring(loc, len);
}
loact = text1.IndexOf(text2);
total++;
}
return total;
}
如果你觉得本方法的算法有待改进,欢迎指正。