1 34 package net.myvietnam.mvncore.util; 35 36 import net.myvietnam.mvncore.exception.BadInputException; 37 import java.util.*; 38 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter; 39 42 public final class StringUtil { 43 44 private StringUtil() { } 46 47 private static final int SHORT_STRING_LENGTH = 100; 48 49 56 public static String [] getStringArray(String inputValue, String delim) { 57 if (inputValue == null) inputValue = ""; 58 inputValue = inputValue.trim(); java.util.StringTokenizer t = new java.util.StringTokenizer (inputValue, delim); 60 String [] ret = new String [t.countTokens()]; 61 int index = 0; 62 while(t.hasMoreTokens()) { 63 String token = t.nextToken().trim(); 64 ret[index] = token; 66 index++; 67 } 68 return ret; 69 } 70 71 public static String [] getStringArrays(String to, String cc, String bcc, String delim) { 72 String [] toMail = getStringArray(to, delim); 73 String [] ccMail = getStringArray(cc, delim); 74 String [] bccMail= getStringArray(bcc, delim); 75 String [] ret = new String [toMail.length + ccMail.length + bccMail.length]; 76 int index = 0; 77 for (int i = 0 ; i < toMail.length; i++) { 78 ret[index] = toMail[i]; 79 index++; 80 } 81 for (int i = 0; i < ccMail.length; i++) { 82 ret[index] = ccMail[i]; 83 index++; 84 } 85 for (int i = 0; i < bccMail.length; i++) { 86 ret[index] = bccMail[i]; 87 index++; 88 } 89 return ret; 90 } 91 92 public static String [] getDiffStringArrays(String to, String cc, String bcc, String delim) { 93 String [] toMail = getStringArray(to, delim); 94 String [] ccMail = getStringArray(cc, delim); 95 String [] bccMail= getStringArray(bcc, delim); 96 Set set = new HashSet(); 98 for (int i = 0 ; i < toMail.length; i++) { 100 set.add(toMail[i]); 101 } 102 for (int i = 0; i < ccMail.length; i++) { 103 set.add(ccMail[i]); 104 } 105 for (int i = 0; i < bccMail.length; i++) { 106 set.add(bccMail[i]); 107 } 108 return (String [])set.toArray(new String [0]); 109 } 110 111 public static String getEmptyStringIfNull(String str) { 112 if (str == null) return ""; 113 return str; 114 } 115 116 122 public static void checkGoodName(String str) throws BadInputException { 123 int length = str.length(); 124 char c = 0; 125 126 for (int i = 0; i < length; i++) { 127 c = str.charAt(i); 128 if ((c >= 'a') && (c <= 'z')) { 129 } else if ((c >= 'A') && (c <= 'Z')) { 131 } else if ((c >= '0') && (c <= '9')) { 133 138 } else if (((c == '_') || (c == '.') || (c == '@')) && (i != 0)) { 140 143 145 } else { 150 throw new BadInputException("The string '" + DisableHtmlTagFilter.filter(str) + "' is not a good name. Reason: character '" + c + "' is not allowed."); 153 } 154 } } 156 157 162 public static String getShorterString(String str) { 163 return getShorterString(str, SHORT_STRING_LENGTH); 164 } 165 166 173 public static String getShorterString(String str, int maxLength) { 174 175 if (maxLength < 0) throw new IllegalArgumentException ("The maxLength < 0 is not allowed."); 176 if (str == null) { 177 return ""; 178 } 179 if (str.length() <= maxLength) { 180 return str; 181 } 182 String s = str.substring(0, maxLength); 183 char currentChar; 184 int index; 185 for (index = s.length() - 1; index >= 0; index--) { 186 currentChar = s.charAt(index); 187 if (Character.isWhitespace(currentChar)) { 188 break; 189 } 190 } 191 String shortString = s.substring(0, index + 1); 192 return shortString + "..."; 193 } 194 195 202 public static String getShorterStringIgnoreSpace(String str, int maxLength) { 203 if (maxLength < 0) throw new IllegalArgumentException ("The maxLength < 0 is not allowed."); 204 if (str == null) return ""; 205 if (str.length() <= maxLength) return str; 206 return str.substring(0, maxLength) + "..."; 207 } 208 209 216 public static String replace(String input, char from, String to) { 217 if (input == null) { 218 return null; 219 } 220 221 char[] s = input.toCharArray(); 222 int length = s.length; 223 StringBuffer ret = new StringBuffer (length * 2); 224 225 for (int i = 0; i < length; i++) { 226 if (s[i] == from) { 227 ret.append(to); 228 } else { 229 ret.append(s[i]); 230 } 231 } return ret.toString(); 233 } 234 235 238 public static Collection getSeparateString(String strContent, String pattern) { 239 int beginIndex = 0; 240 Collection coResult = new ArrayList(); 241 String result; 242 int position = strContent.indexOf(pattern, beginIndex); while (position != -1) { 244 result = strContent.substring(beginIndex, position); 245 if (!result.trim().equals("")) { 246 coResult.add(result); 247 } 248 beginIndex = position + pattern.length(); position = strContent.indexOf(pattern, beginIndex); 250 } 251 252 return coResult; 253 } 254 255 262 public static String getHiddenPassword(String password) { 263 password = getEmptyStringIfNull(password); 264 int length = password.length(); 265 if (length == 0) return password; 266 StringBuffer hiddenPassword = new StringBuffer (length); 267 for (int i = 0; i < length; i++) { 268 hiddenPassword.append('*'); 269 } 270 return hiddenPassword.toString(); 271 } 272 273 public static void main(String [] args) throws Exception { 275 282 String s1 = " abc das\n\n\n\n\ndasd asd adad as das as da adas da sd ad as sa das das d a ."; 283 System.out.println("r = [" + StringUtil.getShorterString(s1, 22) + "]"); 284 } 285 286 } 287 | Popular Tags |