网络上找到了以下代码 然后发现总会丢失一些字符
string MID(string str, string str1, string str2)
{
int i1 = str.IndexOf(str1);
int i2 = -1;
string strok = "";
if (i1 != -1)
{
i2 = str.IndexOf(str2, i1);
}
if (i2 != -1)
{
strok = str.Substring(i1 + 12, i2 - i1 - 12);
}
return strok;
}
之后还是自己写了一段很实用的取出文本中间代码
/// <summary>
/// 截取字符串值
/// </summary>
/// <param name="sourse"></param>
/// <param name="startstr"></param>
/// <param name="endstr"></param>
/// <returns></returns>
string MidStrEx(string sourse, string startstr, string endstr)
{
string result = string.Empty;
int startindex, endindex;
try
{
startindex = sourse.IndexOf(startstr);
if (startindex == -1)
return result;
string tmpstr = sourse.Substring(startindex + startstr.Length);
endindex = tmpstr.IndexOf(endstr);
if (endindex == -1)
return result;
result = tmpstr.Remove(endindex);
}
catch (Exception ex)
{
}
return result;
}
1 条评论
挺好