我的想法是,给定一个字符串,再给出截取字符串开始的特征和结束的特征字符串,return中间的字符串。我写了一个,感觉不好用,谁能帮我完善一下?谢谢了private string search(string a, string begin, string end)
{
if (a != "" && begin != "")
{
try
{
int b = a.IndexOf(begin);
int e = a.IndexOf(end, b);
string t = a.Substring(b, e - b);
t = t.Replace(begin, "");
return t;
}
catch (Exception f)
{
return "";
}
}
else
{
return "";
}
}
{
if (a != "" && begin != "")
{
try
{
int b = a.IndexOf(begin);
int e = a.IndexOf(end, b);
string t = a.Substring(b, e - b);
t = t.Replace(begin, "");
return t;
}
catch (Exception f)
{
return "";
}
}
else
{
return "";
}
}
解决方案 »
- VSTO EXCEL加载项问题
- 刚才搞错了。amandag(高歌) 来取分,仍然感谢lovefootball(蟑螂)
- 一次上传多张图片,奇怪问题
- 急!! C#+SOAPT OOLKit写的winfornm webservice程序打包问题
- [共享3]C#的字符编码的理解,迷惑了我很久,希望会对一部分同样迷惑的人有所帮助(有错误的地方麻烦指出共同讨论)----分散完了呵
- 从表达式数组中取出第一个运算数
- 为什么我在C#的代码里用SQL语句创建一个表的时候,总是会抛出异常,它说这个表已经存在,这是怎么回事?
- 怎样在c#中获得sqlserver存储过程的return值?
- 很急的一个问题,请问如何用C#产生条码,并且能打印出来,谢谢,感激不尽
- 用C#写一个应用程序调用ASP.NET页面,将ASP.NET页面上的数据不停的读入到数据库中(紧急求救!)
- [求助]我想获取一个网页的源代码!请大侠们帮忙啊
- 如何在WinForm中实现WebForm中的操作?
{
int intBegin;
int intEnd; string strRtn = ""; if (a.Length == 0 || begin.Length == 0)
{
return strRtn;
}
try
{
intBegin = a.IndexOf(begin) + begin.Length; if (end.Length != 0)
{
intEnd = a.IndexOf(end, intBegin);
}
else
{
intEnd = a.Length;
} strRtn = a.Substring(intBegin,intEnd - intBegin);
}
catch (Exception ex)
{
strRtn = "";
} return strRtn;
}
private string search(string a, string begin, string end)
{
return System.Text.RegularExpressions.Regex.Replace(a,"("+begin+")(.*?)("+end+")","$2");
}根据需要再自己改改。
{
string strReturn = String.Empty;
int iStartIndex = 0;
int iEndIndex = a.Length; if (begin.Length > 0)
{
iStartIndex = a.IndexOf(begin)+1; if (iStartIndex == -1)
{
iStartIndex = 0;
}
} if (end.Length > 0)
{
iEndIndex = a.LastIndexOf(end); if (iEndIndex == -1)
{
iEndIndex = 0;
}
} if (iStartIndex <= iEndIndex)
{
strReturn= a.Substring(iStartIndex, iEndIndex-iStartIndex);
}
else
{
strReturn= a.Substring(iEndIndex, iStartIndex-iEndIndex);
} return strReturn;
}
{
//如果输入的字符串为空,则返回空
if (a == "")
{
return "";
}
//得到搜索的开始位置a
int b = a.IndexOf(begin) + begin.Length;
//如果结束字符串为空,则返回开始位置到结尾的字符串
if (end.Length == 0)
{
return a.Substring(b, a.Length-b);
}
//搜索结束位置字符串e
int e = a.IndexOf(end, b);
//截取需要的字符串
try
{
string t = a.Substring(b, e - b);
}
catch (Exception f)
{
return "";
}
return t;
}
我还是觉得获得end的index应该用这个