一、去除首尾字符串
///<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); }
三、去掉字符串中的数字
/// <summary> /// 去掉字符串中的数字 /// </summary> /// <param name="key"></param> /// <returns></returns> public static string RemoveNumber(string key) { return System.Text.RegularExpressions.Regex.Replace(key, @"\d", ""); }
四、去掉字符串中的非数字
/// <summary> /// 去掉字符串中的非数字 /// </summary> /// <param name="key"></param> /// <returns></returns> public static string RemoveNotNumber(string key) { return System.Text.RegularExpressions.Regex.Replace(key, @"[^\d]*", ""); }