1. 程式人生 > >String類的一些常用操作方法

String類的一些常用操作方法

pri lis list split exc 數組 value commons build

  1 package com.liveyc.framework.util;
  2 
  3 import java.io.UnsupportedEncodingException;
  4 import java.net.URLDecoder;
  5 import java.text.DecimalFormat;
  6 import java.util.ArrayList;
  7 import java.util.HashMap;
  8 import java.util.Iterator;
  9 import java.util.List;
 10 import java.util.Map;
11 import java.util.StringTokenizer; 12 13 import org.apache.commons.collections.CollectionUtils; 14 15 16 /** 17 * @author aitf 18 * @version 創建時間:2016年12月15日 下午2:38:30 19 * 類說明 20 */ 21 public class StringUtil { 22 23 /** 24 * <li>判斷字符串是否為空值</li> 25 * <li>NULL、空格均認為空值</li>.
26 * 27 * @param value 28 * the value 29 * 30 * @return true, if checks if is empty 31 */ 32 public static boolean isEmpty(String value) { 33 return null == value || "".equals(value.trim()); 34 } 35 36 37 /** 38 * 去除,分隔符,用於金額數值去格式化.
39 * 40 * @param value 41 * the value 42 * 43 * @return the string 44 */ 45 public static String decimal(String value) { 46 if (null == value || "".equals(value.trim())) { 47 return "0"; 48 } else { 49 return value.replaceAll(",", ""); 50 } 51 } 52 53 /** 54 * 在數組中查找字符串. 55 * 56 * @param params 57 * the params 58 * @param name 59 * the name 60 * @param ignoreCase 61 * the ignore case 62 * 63 * @return the int 64 */ 65 public static int indexOf(String[] params, String name, boolean ignoreCase) { 66 if (params == null) 67 return -1; 68 for (int i = 0, j = params.length; i < j; i++) { 69 if (ignoreCase && params[i].equalsIgnoreCase(name)) { 70 return i; 71 } else if (params[i].equals(name)) { 72 return i; 73 } 74 } 75 return -1; 76 } 77 78 /** 79 * 查詢Str2在Str1中出現幾次 80 * 81 * @param str1 82 * @param str2 83 * @return 84 */ 85 public static int indexAllOf(String str1, String str2) { 86 int he = 0; 87 for (int i = 0; i < str1.length(); i++) { 88 int t = str1.indexOf(str2, i); 89 if (i == t) { 90 he++; 91 } 92 } 93 return he; 94 } 95 96 /** 97 * 將字符轉數組. 98 * 99 * @param str 100 * the str 101 * 102 * @return the string[] 103 */ 104 public static String[] toArr(String str) { 105 String inStr = str; 106 String a[] = null; 107 try { 108 if (null != inStr) { 109 StringTokenizer st = new StringTokenizer(inStr, ","); 110 if (st.countTokens() > 0) { 111 a = new String[st.countTokens()]; 112 int i = 0; 113 while (st.hasMoreTokens()) { 114 a[i++] = st.nextToken(); 115 } 116 } 117 } 118 } catch (Exception e) { 119 e.printStackTrace(); 120 } 121 return a; 122 } 123 124 /** 125 * 將字符轉數組. 126 * 127 * @param str 128 * the str 129 * @param splitChar 130 * the split char 131 * 132 * @return the string[] 133 */ 134 public static String[] toArr(String str, String splitChar) { 135 String inStr = str; 136 String a[] = null; 137 try { 138 if (null != inStr) { 139 StringTokenizer st = new StringTokenizer(inStr, splitChar); 140 if (st.countTokens() > 0) { 141 a = new String[st.countTokens()]; 142 int i = 0; 143 while (st.hasMoreTokens()) { 144 a[i++] = st.nextToken(); 145 } 146 } 147 } 148 } catch (Exception e) { 149 e.printStackTrace(); 150 } 151 return a; 152 } 153 154 /** 155 * 字符串數組包裝成字符串. 156 * 157 * @param ary 158 * the ary 159 * @param s 160 * 包裝符號如 ‘ 或 " 161 * 162 * @return the string 163 */ 164 public static String toStr(String[] ary, String s) { 165 if (ary == null || ary.length < 1) 166 return ""; 167 StringBuffer bf = new StringBuffer(); 168 bf.append(s); 169 bf.append(ary[0]); 170 for (int i = 1; i < ary.length; i++) { 171 bf.append(s).append(",").append(s); 172 bf.append(ary[i]); 173 } 174 bf.append(s); 175 return bf.toString(); 176 } 177 178 /** 179 * 得到字符的編碼格式 180 * 181 * @param str 182 * @return 183 */ 184 public static String getEncoding(String str) { 185 String encode = "GB2312"; 186 try { 187 if (str.equals(new String(str.getBytes(encode), encode))) { 188 String s = encode; 189 return s; 190 } 191 } catch (Exception exception) { 192 } 193 encode = "ISO-8859-1"; 194 try { 195 if (str.equals(new String(str.getBytes(encode), encode))) { 196 String s1 = encode; 197 return s1; 198 } 199 } catch (Exception exception1) { 200 } 201 encode = "UTF-8"; 202 try { 203 if (str.equals(new String(str.getBytes(encode), encode))) { 204 String s2 = encode; 205 return s2; 206 } 207 } catch (Exception exception2) { 208 } 209 encode = "GBK"; 210 try { 211 if (str.equals(new String(str.getBytes(encode), encode))) { 212 String s3 = encode; 213 return s3; 214 } 215 } catch (Exception exception3) { 216 } 217 return ""; 218 } 219 220 221 /** 222 * utf8轉碼 Description :. 223 * 224 * @param str 225 * the str 226 * 227 * @return the string 228 */ 229 public static String utf8Decoder(String str) { 230 try { 231 if (str != null) { 232 return URLDecoder.decode(str, "UTF-8"); 233 } 234 } catch (UnsupportedEncodingException e) { 235 e.printStackTrace(); 236 } 237 return str; 238 } 239 240 241 public static String changeCharset(String str, String oldCharset, String newCharset) 242 throws UnsupportedEncodingException { 243 if (str != null) { 244 // 用舊的字符編碼解碼字符串。解碼可能會出現異常。 245 byte[] bs = str.getBytes(oldCharset); 246 // 用新的字符編碼生成字符串 247 return new String(bs, newCharset); 248 } 249 return null; 250 } 251 252 253 /** 254 * 過濾掉高亮的html 255 * @param str 256 * @return 257 */ 258 public static String htmlFilter(String str) { 259 if (isEmpty(str)) { 260 return str; 261 } 262 str = str.replace("<font color=‘red‘>", ""); 263 str = str.replace("<font color=‘blue‘>", ""); 264 str = str.replace("</font>", ""); 265 266 return str; 267 } 268 269 public static String trimString(String str){ 270 if(isEmpty(str)){ 271 return str; 272 } 273 return str.trim(); 274 } 275 276 277 public static String encodeToUtf(String str) throws Exception { 278 if(isEmpty(str)){ 279 return str; 280 } 281 return new String(str.getBytes("iso-8859-1"), "UTF-8"); 282 } 283 284 /** 285 * 根據身份證號轉性別 286 * @param sfzh 287 * @return 288 */ 289 public static String converToSex(String sfzh){ 290 int sex = 0; 291 if(StringUtil.isEmpty(sfzh)){ 292 return ""; 293 }else{ 294 if(sfzh.length()==15){ 295 sex = Integer.parseInt(sfzh.substring(13,14)); 296 }else if(sfzh.length()==18){ 297 sex = Integer.parseInt(sfzh.substring(16,17)); 298 } 299 if(sex%2 == 0){ 300 return "女"; 301 }else{ 302 return "男"; 303 } 304 } 305 } 306 307 /** 308 * 設置地址的Map,並去重 309 * @param addrMap 310 * @param fromType 311 * @param addrs 312 */ 313 public static void setAddr2Map(Map addrMap,String addrs,String fromType){ 314 String[] addrls = null ; 315 if(addrMap==null){ 316 addrMap = new HashMap(); 317 } 318 if(addrMap.containsKey(fromType)){ 319 String strAddr = (String)addrMap.get(fromType); 320 if(strAddr!=null && strAddr.trim().length()>0){ 321 addrls = strAddr.split(","); 322 } 323 324 if(!isExsit(addrls,addrs)){ 325 strAddr +=","+addrs; 326 addrMap.put(fromType, strAddr); 327 } 328 }else{ 329 addrMap.put(fromType, addrs); 330 } 331 } 332 333 334 /** 335 * 字符口串是否在數據組存在 336 * @param addrls 337 * @param addrs 338 * @return 339 */ 340 private static boolean isExsit(String[] addrls,String addrs){ 341 if(addrls!=null && addrls.length>0){ 342 for(int i=0;i<addrls.length;i++){ 343 if(addrls[i].equals(addrs)){ 344 return true; 345 } 346 } 347 } 348 return false; 349 } 350 /** 351 * 把Map轉換成String 352 * @param addrMap 353 * @return 354 */ 355 public static String convMap2String(Map addrMap){ 356 StringBuilder tempBuf =new StringBuilder(); 357 Iterator<Map.Entry> it = addrMap.entrySet().iterator(); 358 while (it.hasNext()) { 359 Map.Entry<String, String> entry = it.next(); 360 String fldName = entry.getKey(); 361 String fldValue = entry.getValue(); 362 tempBuf.append(fldValue).append("(").append(fldName).append(");"); 363 } 364 return tempBuf.toString(); 365 } 366 367 //字節轉換 368 public static String formetFileSize(long fileS) { 369 370 DecimalFormat df = new DecimalFormat("#.00"); 371 String fileSizeString = ""; 372 if (fileS < 1024) { 373 fileSizeString = df.format((long) fileS) + "B"; 374 } else if (fileS < 1048576) { 375 fileSizeString = df.format((long) fileS / 1024) + "KB"; 376 } else if (fileS < 1073741824) { 377 fileSizeString = df.format((long) fileS / 1048576) + "MB"; 378 } else if(fileS < 1099511627776l) { 379 fileSizeString = df.format((long) fileS / 1073741824) + "GB"; 380 } else{ 381 fileSizeString = df.format((long) fileS / 1099511627776l) + "TB"; 382 } 383 return fileSizeString; 384 } 385 386 public static void main(String[] args) { 387 388 long a = 1948065583104l; 389 System.out.println(formetFileSize(a)); 390 } 391 392 }

String類的一些常用操作方法