1 4 package com.tc.util; 5 6 import java.util.Arrays ; 7 8 public class StringUtil { 9 10 public static final char SPACE = ' '; 11 public static final String SPACE_STRING = " "; 12 13 public static final String EMPTY = ""; 14 15 public static final String NULL_STRING = "<null>"; 16 17 public static final boolean isNullOrBlank(String s) { 18 return s == null || EMPTY.equals(s.trim()); 19 } 20 21 public static final String safeToString(Object object) { 22 return object != null ? object.toString() : NULL_STRING; 23 } 24 25 32 public static final boolean exists(String [] list, String value) { 33 if (list != null) { 34 for (int pos = 0; pos < list.length; ++pos) { 35 if (list[pos] == value || (list[pos] != null && value != null && list[pos].equals(value))) { return true; } 36 } 37 } 38 return false; 39 } 40 41 45 public static String ordinal(long position) { 46 long mod10 = Math.abs(position) % 10; 47 long mod100 = Math.abs(position) % 100; 48 StringBuffer rv = new StringBuffer (EMPTY + position); 49 if (mod10 == 1) { 50 rv.append(mod100 == 11 ? "th" : "st"); 51 } else if (mod10 == 2) { 52 rv.append(mod100 == 12 ? "th" : "nd"); 53 } else if (mod10 == 3) { 54 rv.append(mod100 == 13 ? "th" : "rd"); 55 } else { 56 rv.append("th"); 57 } 58 return rv.toString(); 59 } 60 61 public static String indentLines(String source) { 62 return indentLines(source, 1); 63 } 64 65 public static String indentLines(String source, int indentLevel) { 66 return indentLines(new StringBuffer (source), indentLevel).toString(); 67 } 68 69 public static StringBuffer indentLines(StringBuffer source, int indentLevel) { 70 return indentLines(source, indentLevel, '\t'); 71 } 72 73 public static StringBuffer indentLines(StringBuffer source, int indentLevel, char indentChar) { 74 if ((source == null) || (indentLevel == 0)) { return source; } 75 76 final String indentStr; 77 78 if (indentLevel <= 0) { 79 throw new IllegalArgumentException ("Negative indentation not supported"); 81 } else if (indentLevel > 1) { 82 char[] chars = new char[indentLevel]; 83 Arrays.fill(chars, indentChar); 84 indentStr = new String (chars); 85 } else { 86 89 indentStr = new String (new char[] { indentChar }); 90 } 91 92 source.insert(0, indentStr); 93 94 int index = 0; 95 while ((index = indexOfStringBuffer(source, "\n", index)) != -1) { 96 index++; 97 if (index == source.length()) { 98 break; 99 } 100 source.insert(index, indentStr); 101 } 102 103 return source; 104 } 105 106 public static int indexOfStringBuffer(StringBuffer source, String search, int start) { 107 return source.toString().indexOf(search, start); 108 } 109 110 120 public static final String toString(Object [] objs, String separator, String prefix, String postfix) { 121 StringBuffer rv = new StringBuffer (); 122 if (objs != null) { 123 for (int pos = 0; pos < objs.length; ++pos) { 124 if (rv.length() > 0 && separator != null) { 125 rv.append(separator); 126 } 127 if (prefix != null) { 128 rv.append(prefix); 129 } 130 rv.append(objs[pos] != null ? objs[pos].toString() : "null"); 131 if (postfix != null) { 132 rv.append(postfix); 133 } 134 } 135 } else { 136 rv.append(NULL_STRING); 137 } 138 return rv.toString(); 139 } 140 141 public static final String toString(int[] objs, String separator, String prefix, String postfix) { 142 StringBuffer rv = new StringBuffer (); 143 if (objs != null) { 144 for (int pos = 0; pos < objs.length; ++pos) { 145 if (rv.length() > 0 && separator != null) { 146 rv.append(separator); 147 } 148 if (prefix != null) { 149 rv.append(prefix); 150 } 151 rv.append(objs[pos]); 152 if (postfix != null) { 153 rv.append(postfix); 154 } 155 } 156 } else { 157 rv.append(NULL_STRING); 158 } 159 return rv.toString(); 160 } 161 162 public static final String toPaddedString(long value, int radix, int paddedWidth) { 163 StringBuffer result = new StringBuffer (); 164 String strValue = Long.toString(value, radix); 165 for (int pos = 0; pos < paddedWidth - strValue.length(); ++pos) { 166 result.append("0"); 167 } 168 result.append(strValue); 169 return result.toString(); 170 } 171 172 public static final String rightJustify(String s, int fieldSize) { 173 if (s.length() == fieldSize) return s; 174 if (s.length() > fieldSize) { 175 final int i = Math.max(s.length() - (fieldSize - 3), 1); 176 return leftPad(s.substring(i), fieldSize, '.'); 177 } 178 return leftPad(s, fieldSize, ' '); 179 } 180 181 public static final String rightPad(String s, int size, char padChar) { 182 if (s.length() >= size) return s; 183 final int padCount = size - s.length(); 184 StringBuffer sb = new StringBuffer (); 185 sb.append(s); 186 for (int i = 0; i < padCount; i++) { 187 sb.append(padChar); 188 } 189 String rv = sb.toString(); 190 Assert.eval(rv.length() == size); 191 return rv; 192 } 193 194 public static final String leftPad(String s, int size, char padChar) { 195 if (s.length() >= size) return s; 196 final int padCount = size - s.length(); 197 StringBuffer sb = new StringBuffer (); 198 for (int i = 0; i < padCount; i++) { 199 sb.append(padChar); 200 } 201 sb.append(s); 202 String rv = sb.toString(); 203 Assert.eval(rv.length() == size); 204 return rv; 205 } 206 207 219 public static final String replaceAll(String source, String search, String replace, boolean skipQuotedStrings) { 220 if (source == null || search == null) { return null; } 221 StringBuffer result = new StringBuffer (); 222 int beginQuoteIdx = 0; 223 for (int pos = 0; pos < source.length(); ++pos) { 224 if (skipQuotedStrings && source.startsWith("'", pos)) { 225 beginQuoteIdx = pos; 227 for (++pos; pos < source.length() && !source.startsWith("'", pos); ++pos) { 228 } 230 231 result.append(source.substring(beginQuoteIdx, pos + 1)); 232 } else if (skipQuotedStrings && source.startsWith("\"", pos)) { 233 beginQuoteIdx = pos; 235 for (++pos; pos < source.length() && !source.startsWith("\"", pos); ++pos) { 236 } 238 result.append(source.substring(beginQuoteIdx, pos + 1)); 239 } else if (source.startsWith(search, pos)) { 240 if (replace != null) { 241 result.append(replace); 242 } 243 pos += search.length() - 1; 244 } else { 245 result.append(source.charAt(pos)); 246 } 247 } 248 return result.toString(); 249 } 250 } | Popular Tags |