String字符,这是最常见的类型,但是时常又需要对字符进行处理,系统自带的有时并不能满足大伙的需求,以下列举出一些String加强的方法,Function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
public class StringPlus { //---字符串转化为list public static List<string> GetStrArray(string str, char speater,bool toLower) { List<string> list = new List<string>(); string[] ss = str.Split(speater); foreach (string s in ss) { if (!string.IsNullOrEmpty(s) &&s !=speater.ToString()) { string strVal = s; if (toLower) { strVal = s.ToLower(); } list.Add(strVal); } } return list; } //---字符串转化为list public static List<string> GetSubStringList(string o_str, char sepeater) { List<string> list = new List<string>(); string[] ss = o_str.Split(sepeater); foreach (string s in ss) { if (!string.IsNullOrEmpty(s) && s != sepeater.ToString()) { list.Add(s); } } return list; } //---字符串转化为数组 public static string[] GetStrArray(string str) { return str.Split(new char[',']); } //---list转化为字符串 public static string GetArrayStr(List<string> list,string speater) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.Count; i++) { if (i == list.Count - 1) { sb.Append(list[i]); } else { sb.Append(list[i]); sb.Append(speater); } } return sb.ToString(); } #region 删除最后一个字符之后的字符 //--- 删除最后结尾的一个逗号 public static string DelLastComma(string str) { return str.Substring(0, str.LastIndexOf(",")); } //--- 删除最后结尾的指定字符后的字符 public static string DelLastChar(string str,string strchar) { return str.Substring(0, str.LastIndexOf(strchar)); } #endregion //--- 转全角的函数(SBC case) public static string ToSBC(string input) { //半角转全角: char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) { if (c[i] == 32) { c[i] = (char)12288; continue; } if (c[i] < 127) c[i] = (char)(c[i] + 65248); } return new string(c); } //--- 转半角的函数(SBC case) public static string ToDBC(string input) { char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) { if (c[i] == 12288) { c[i] = (char)32; continue; } if (c[i] > 65280 && c[i] < 65375) c[i] = (char)(c[i] - 65248); } return new string(c); } #region 将字符串样式转换为纯字符串 public static string GetCleanStyle(string StrList, string SplitString) { string RetrunValue = ""; //如果为空,返回空值 if (StrList == null) { RetrunValue = ""; } else { //返回去掉分隔符 string NewString = ""; NewString = StrList.Replace(SplitString, ""); RetrunValue = NewString; } return RetrunValue; } #endregion #region 将字符串转换为新样式 public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error) { string ReturnValue = ""; //如果输入空值,返回空,并给出错误提示 if (StrList == null) { ReturnValue = ""; Error = "请输入需要划分格式的字符串"; } else { //检查传入的字符串长度和样式是否匹配,如果不匹配,则说明使用错误。给出错误信息并返回空值 int strListLength = StrList.Length; int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length; if (strListLength != NewStyleLength) { ReturnValue = ""; Error = "样式格式的长度与输入的字符长度不符,请重新输入"; } else { //检查新样式中分隔符的位置 string Lengstr = ""; for (int i = 0; i < NewStyle.Length; i++) { if (NewStyle.Substring(i, 1) == SplitString) { Lengstr = Lengstr + "," + i; } } if (Lengstr != "") { Lengstr = Lengstr.Substring(1); } //将分隔符放在新样式中的位置 string[] str = Lengstr.Split(','); foreach (string bb in str) { StrList = StrList.Insert(int.Parse(bb), SplitString); } //给出最后的结果 ReturnValue = StrList; //因为是正常的输出,没有错误 Error = ""; } } return ReturnValue; } #endregion /// 截取字符 public static string SubString(string s, int len, string endStr) { if (string.IsNullOrEmpty(s)) { return string.Empty; } string temp = s.Substring(0, (s.Length < len + 1) ? s.Length : len + 1); byte[] encodedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(temp); string outputStr = ""; int count = 0; for (int i = 0; i < temp.Length; i++) { if ((int)encodedBytes[i] == 63) count += 2; else count += 1; if (count <= len - endStr.Length) outputStr += temp.Substring(i, 1); else if (count > len) break; } if (count <= len) { outputStr = temp; endStr = ""; } outputStr += endStr; return outputStr; } /// <summary> /// 用于删除指定位置的指定字符 /// </summary> /// <param name="_baseString">要处理的原始字符串</param> /// <param name="_removeIndex">要删除的字符开始位置</param> /// <param name="_char">要删除的字符</param> /// <returns>处理后的字符串</returns> public static string RemoveChar(string _baseString, int _removeIndex, char _char) { if (string.IsNullOrEmpty(_baseString)) { return ""; } else { if (_baseString.Contains(_char.ToString()) && _baseString[_removeIndex] == _char) { return _baseString.Substring(_removeIndex, _char.ToString().Length); } else { return _baseString; } } } /// <summary> /// 在字符串参数中检查指定的字符出现的次数 /// </summary> /// <param name="InputStr">待检查的字符串</param> /// <param name="Char">指定的字符</param> /// <returns>返回该字符出现的次数,没找到就返回-1</returns> public static int FindChar(string InputStr, char Char) { if (!string.IsNullOrEmpty(InputStr)) { int idx = 0; char[] CharArr = InputStr.ToCharArray(); foreach (char inputchar in CharArr) { if (Char == inputchar) idx += 1; } if (idx != 0) return idx; } return -1; } /// <summary> /// 返回字符串的Ascii字符数量(中文*2英文*1) /// </summary> /// <param name="strCode">查询字符串</param> /// <returns>Ascii字符数</returns> public static int StrLen(string strCode) { int _strlength = strCode.Length; int tmpNum = 0; byte[] strASCII = ASCIIEncoding.ASCII.GetBytes(strCode); for (int i = 0; i < _strlength; i++) { if ((int)strASCII[i] == 63) { tmpNum += 2; } else { tmpNum += 1; } if (i == _strlength - 1) break; } return tmpNum; } //--- 让标签支持空格、逗号、中文空格、中文逗号 public static string Tag(string str) { str = str.Replace(" ", ","); //英文空格转逗号 str = str.Replace(",", ",");//中文逗号转逗号 str = str.Replace(" ", ",");//中文空格转逗号 return str; } //--- 文本转换成HTML public static string Encode(string str) { str = str.Replace("&", "&"); str = str.Replace("'", "''"); str = str.Replace("\"", """); str = str.Replace(" ", " "); str = str.Replace("<", "<"); str = str.Replace(">", ">"); str = str.Replace("\n", "<br>"); return str; } //--- html转成 普通文本 public static string Decode(string str) { str = str.Replace("<br>", "\n"); str = str.Replace(">", ">"); str = str.Replace("<", "<"); str = str.Replace(" ", " "); str = str.Replace(""", "\""); return str; } //--- 处理SQL注入字符串 public static string CheckSqlStr(string str) { if (str == null || str == "") return ""; str = str.ToLower(); str = str.Replace(",", ""); str = str.Replace(",", ""); str = str.Replace("'", ""); str = str.Replace("‘", ""); str = str.Replace("’", ""); str = str.Replace("@", ""); str = str.Replace("@", ""); str = str.Replace(".", ""); str = str.Replace("。", ""); str = str.Replace(" ", "");//处理空格 (两个空格就可以了) str = str.Replace("<", "");//处理小于号 str = str.Replace(">", "");//处理大于号 str = str.Replace(";", ""); str = str.Replace("'", ""); str = str.Replace("&", ""); str = str.Replace("%20", ""); str = str.Replace("--", ""); str = str.Replace("==", ""); str = str.Replace("%", ""); str = str.Replace("(", ""); str = str.Replace(")", ""); return str; } } |