usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceHh.Mes.Common{publicstaticclassStringExtension{///<summary>针对单个格式字符串调用"welcome to {0}! welcome to {1}!".FormatWith("lsw","wisdom");未做null判断///</summary>publicstaticstringFormatWith(thisstringformat,paramsobject[]args){varcapacity=format.Length+args.Select(p=>p.ToString()).Sum(p=>p.Length);Console.WriteLine(capacity);varstringBuilder=newStringBuilder(capacity);stringBuilder.AppendFormat(format,args);returnstringBuilder.ToString();}///<summary>针对多个格式字符串newstring[]{"welcome to {0}!"," welcome to {1}!"}.FormatWith("lsw","wisdom");///</summary>publicstaticstringFormatWith(thisIEnumerable<string>formats,paramsobject[]args){varcapacity=formats.Where(f=>!string.IsNullOrEmpty(f)).Sum(f=>f.Length)+args.Where(a=>a!=null).Select(p=>p.ToString()).Sum(p=>p.Length);varstringBuilder=newStringBuilder(capacity);foreach(varfinformats){if(!string.IsNullOrEmpty(f)){stringBuilder.AppendFormat(f,args);}}returnstringBuilder.ToString();}///<summary>///字符特殊符号替换///</summary>publicstaticstringStrJsonReplace(thisstringstr){varsb=newStringBuilder();for(inti=0;i<str.Length;i++){varc=str[i];switch(c){case'\"':case'\'':case'\\':case'/':case'\b':case'\f':case'\n':case'\r':case'\t':sb.Append(" ");break;default:sb.Append(c);break;}}returnsb.ToString().Replace("CNOOCLegLineUAT"," ").Replace("SqlSugar","xxx").Replace("Sugar","xxx").Replace("Sql","xxx");}}}