C#去掉字符串中的特殊字符
方案一:
string except_chars = ": ‘ ! @ # % … & * ( ^ & ¥ , 。 , .)$";
string src = "就是包含: 这些‘字符 包含空格)都要$去掉么?";
string result = Regex.Replace(src, "[" + Regex.Escape(except_chars) + "]", "");
方案二:
//只保留字母、数字 和汉字
string strAfter= Regex.Replace(strBefor, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", "");
实例:
/// <summary> /// 判断该样本商品是否已存在 /// </summary> /// <param name="model"></param> /// <returns>存在:true</returns> public bool IsExist(ClassifyCorrectionPanelPool model) { bool isExist = false; List<ClassifyCorrectionPanelPool> modelList = LoadEntities(a => a.CODE_TS.Equals(model.CODE_TS) && a.G_NAME.Equals(model.G_NAME)).ToList(); if (modelList.Count > 0) { foreach (var item in modelList) { string GModelrep = Regex.Replace(item.G_MODEL, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", ""); string addGModelRep = Regex.Replace(model.G_MODEL, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", ""); if (GModelrep.Equals(addGModelRep)) { isExist= true; } } } return isExist; }