1 18 package org.apache.geronimo.interop.util; 19 20 public abstract class StringUtil { 21 public static boolean endsWithIgnoreCase(String name, String suffix) { 22 return name.toLowerCase().endsWith(suffix.toLowerCase()); 23 } 24 25 public static String afterFirst(String separator, String str) { 26 int pos = str.indexOf(separator); 27 if (pos != -1) { 28 return str.substring(pos + separator.length()); 29 } else { 30 return str; 31 } 32 } 33 34 public static String afterLast(String separator, String str) { 35 int pos = str.lastIndexOf(separator); 36 if (pos != -1) { 37 return str.substring(pos + separator.length()); 38 } else { 39 return str; 40 } 41 } 42 43 public static String beforeFirst(String separator, String str) { 44 int pos = str.indexOf(separator); 45 if (pos != -1) { 46 return str.substring(0, pos); 47 } else { 48 return str; 49 } 50 } 51 52 public static String beforeLast(String separator, String str) { 53 int pos = str.lastIndexOf(separator); 54 if (pos != -1) { 55 return str.substring(0, pos); 56 } else { 57 return str; 58 } 59 } 60 61 public static String afterLastSlashOrDot(String str) { 62 int pos = str.lastIndexOf('/'); 63 int dot = str.lastIndexOf('.'); 64 if (pos != -1 && dot > pos) { 65 pos = dot; 66 } 67 if (pos == -1) { 68 return ""; 69 } else { 70 return str.substring(pos + 1); 71 } 72 } 73 74 public static boolean equalOrBothNull(String a, String b) { 75 if (a == null) { 76 return b == null; 77 } else { 78 return b != null && a.equals(b); 79 } 80 } 81 82 85 public static String getJavaIdentifier(String str) { 86 int n = str.length(); 88 StringBuffer s = new StringBuffer (n); 89 for (int i = 0; i < n; i++) { 90 char c = str.charAt(i); 91 if (c == '_') { 92 s.append("__"); 93 } else if (i == 0) { 94 if (!Character.isJavaIdentifierStart(c)) { 95 s.append("_"); 96 } else { 97 s.append(c); 98 } 99 } else { 100 if (!Character.isJavaIdentifierPart(c)) { 101 s.append('_'); 102 s.append((int) c); 103 s.append('_'); 104 } else { 105 s.append(c); 106 } 107 } 108 } 109 return s.toString(); 110 } 111 112 public static String getLowerFirst(String name) { 113 if (name.length() == 0) { 114 return name; 115 } else { 116 return Character.toLowerCase(name.charAt(0)) + name.substring(1); 117 } 118 } 119 120 public static String getLowerFirstIfFirst2NotUpper(String name) { 121 if (name.length() >= 2) { 122 char c1 = name.charAt(0); 123 char c2 = name.charAt(1); 124 if (Character.isUpperCase(c1) && Character.isUpperCase(c2)) { 125 return name; 126 } 127 } 128 return getLowerFirst(name); 129 } 130 131 public static String getUpperFirst(String name) { 132 if (name.length() == 0) { 133 return name; 134 } else { 135 return Character.toUpperCase(name.charAt(0)) + name.substring(1); 136 } 137 } 138 139 public static String padLeft(String s, char c, int n) { 140 if (s.length() < n) { 141 StringBuffer sb = new StringBuffer (n); 142 padLeftAppend(sb, s, c, n); 143 return sb.toString(); 144 } else { 145 return s; 146 } 147 } 148 149 public static void padLeftAppend(StringBuffer sb, String s, char c, int n) { 150 int p = n - s.length(); 151 for (int i = 0; i < p; i++) { 152 sb.append(c); 153 } 154 sb.append(s); 155 } 156 157 public static String padRight(String s, char c, int n) { 158 if (s.length() < n) { 159 StringBuffer sb = new StringBuffer (n); 160 padRightAppend(sb, s, c, n); 161 return sb.toString(); 162 } else { 163 return s; 164 } 165 } 166 167 public static void padRightAppend(StringBuffer sb, String s, char c, int n) { 168 sb.append(s); 169 int p = n - s.length(); 170 for (int i = 0; i < p; i++) { 171 sb.append(c); 172 } 173 } 174 175 public static boolean startsWithIgnoreCase(String name, String prefix) { 176 return name.toLowerCase().startsWith(prefix.toLowerCase()); 177 } 178 179 public static String removePrefix(String name, String prefix) { 180 if (name.startsWith(prefix)) { 181 return name.substring(prefix.length()); 182 } else { 183 return name; 184 } 185 } 186 187 public static String removePrefixIgnoreCase(String name, String prefix) { 188 if (startsWithIgnoreCase(name, prefix)) { 189 return name.substring(prefix.length()); 190 } else { 191 return name; 192 } 193 } 194 195 public static String removeSuffix(String name, String suffix) { 196 if (name.endsWith(suffix)) { 197 return name.substring(0, name.length() - suffix.length()); 198 } else { 199 return name; 200 } 201 } 202 203 public static String removeSuffixIgnoreCase(String name, String suffix) { 204 if (endsWithIgnoreCase(name, suffix)) { 205 return name.substring(0, name.length() - suffix.length()); 206 } else { 207 return name; 208 } 209 } 210 211 214 public static String replace(String str, String what, String with) { 215 int pos = str.indexOf(what); 216 if (pos == -1) { 217 return str; 218 } 219 return str.substring(0, pos) + with + replace(str.substring(pos + what.length()), what, with); 220 } 221 222 225 public static String replaceFirst(String str, String what, String with) { 226 int pos = str.indexOf(what); 227 if (pos == -1) { 228 return str; 229 } 230 return str.substring(0, pos) + with + str.substring(pos + what.length()); 231 } 232 233 public static String reverse(String str) { 234 int n = str.length(); 235 char[] chars = new char[n]; 236 for (int i = 0; i < n; i++) { 237 chars[i] = str.charAt(n - i - 1); 238 } 239 return new String (chars); 240 } 241 242 public static String trimIfNotNull(String str) { 243 return str == null ? null : str.trim(); 244 } 245 } 246 | Popular Tags |