一、去除首尾字符串
代碼如下:
///<summary>
/// 截前後字符(串)
///</summary>
///<param name="val">原字符串</param>
///<param name="str">要截掉的字符串</param>
///<param name="all">是否貪婪</param>
///<returns></returns>
private string GetString(string val,string str,bool all)
{
return Regex.Replace(val, @"(^(" + str + ")" + (all ? "*" : "") + "|(" + str + ")"+(all ? "*" : "") + "$)", "");
}
二、去除首尾字符
代碼如下:
/// <summary>
///
/// </summary>
/// <param name="val">原字符串</param>
/// <param name="c">要截取的字符</param>
/// <returns></returns>
private string GetString(string val,char c)
{
return val.TrimStart(c).TrimEnd(c);
}