1 21 22 package org.apache.derby.iapi.util; 23 import java.util.Locale ; 24 25 28 public class StringUtil 29 { 30 34 public final static String formatForPrint(String input) 35 { 36 if (input.length() > 60) 37 { 38 StringBuffer tmp = new StringBuffer (input.substring(0, 60)); 39 tmp.append("&"); 40 input = tmp.toString(); 41 } 42 return input; 43 } 44 45 49 public static String [] toStringArray(Object [] objArray) { 50 int idx; 51 int len=objArray.length; 52 String [] strArray=new String [len]; 53 54 for (idx=0 ; idx<len ; idx++) { 55 strArray[idx]=objArray[idx].toString(); 56 } 57 58 return strArray; 59 } 60 61 86 public static byte[] getAsciiBytes(String input) 87 { 88 char[] c = input.toCharArray(); 89 byte[] b = new byte[c.length]; 90 for (int i = 0; i < c.length; i++) 91 b[i] = (byte)(c[i] & 0x007F); 92 93 return b; 94 } 95 96 103 public static String trimTrailing( String str) 104 { 105 if( str == null) 106 return null; 107 int len = str.length(); 108 for( ; len > 0; len--) 109 { 110 if( ! Character.isWhitespace( str.charAt( len - 1))) 111 break; 112 } 113 return str.substring( 0, len); 114 } 116 117 127 public static String truncate(String value, int length) 128 { 129 if (value != null && value.length() > length) 130 value = value.substring(0, length); 131 return value; 132 } 133 134 144 public static String slice(String value, 145 int beginOffset, int endOffset, 146 boolean trim) 147 { 148 String retval = value.substring(beginOffset, endOffset + 1); 149 150 if (trim) 151 retval = retval.trim(); 152 153 return retval; 154 } 155 156 157 private static char[] hex_table = { 158 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 159 'a', 'b', 'c', 'd', 'e', 'f' 160 }; 161 162 163 179 public static String toHexString(byte[] data, int offset, int length) 180 { 181 StringBuffer s = new StringBuffer (length*2); 182 int end = offset+length; 183 184 for (int i = offset; i < end; i++) 185 { 186 int high_nibble = (data[i] & 0xf0) >>> 4; 187 int low_nibble = (data[i] & 0x0f); 188 s.append(hex_table[high_nibble]); 189 s.append(hex_table[low_nibble]); 190 } 191 192 return s.toString(); 193 } 194 195 212 public static byte[] toHexByte(String str, int offset, int length) 213 { 214 byte[] data = new byte[(length - offset) * 2]; 215 int end = offset+length; 216 217 for (int i = offset; i < end; i++) 218 { 219 char ch = str.charAt(i); 220 int high_nibble = (ch & 0xf0) >>> 4; 221 int low_nibble = (ch & 0x0f); 222 data[i] = (byte)high_nibble; 223 data[i+1] = (byte)low_nibble; 224 } 225 return data; 226 } 227 228 239 public static byte[] fromHexString(String s, int offset, int length) 240 { 241 if ((length%2) != 0) 242 return null; 243 244 byte[] byteArray = new byte[length/2]; 245 246 int j = 0; 247 int end = offset+length; 248 for (int i = offset; i < end; i += 2) 249 { 250 int high_nibble = Character.digit(s.charAt(i), 16); 251 int low_nibble = Character.digit(s.charAt(i+1), 16); 252 253 if (high_nibble == -1 || low_nibble == -1) 254 { 255 return null; 257 } 258 259 byteArray[j++] = (byte)(((high_nibble << 4) & 0xf0) | (low_nibble & 0x0f)); 260 } 261 return byteArray; 262 } 263 266 public static String hexDump(byte[] data) 267 { 268 byte byte_value; 269 270 271 StringBuffer str = new StringBuffer (data.length * 3); 272 273 str.append("Hex dump:\n"); 274 275 for (int i = 0; i < data.length; i += 16) 276 { 277 String offset = Integer.toHexString(i); 279 280 for (int offlen = offset.length(); offlen < 8; offlen++) 282 str.append("0"); 283 str.append(offset); 284 str.append(":"); 285 286 for (int j = 0; (j < 16) && ((i + j) < data.length); j++) 288 { 289 byte_value = data[i + j]; 290 291 if ((j % 2) == 0) 293 str.append(" "); 294 295 byte high_nibble = (byte) ((byte_value & 0xf0) >>> 4); 297 byte low_nibble = (byte) (byte_value & 0x0f); 298 299 str.append(hex_table[high_nibble]); 300 str.append(hex_table[low_nibble]); 301 } 302 303 str.append(" "); 305 306 for (int j = 0; (j < 16) && ((i + j) < data.length); j++) 307 { 308 char char_value = (char) data[i + j]; 309 310 if (Character.isLetterOrDigit(char_value)) 312 str.append(String.valueOf(char_value)); 313 else 314 str.append("."); 315 } 316 317 str.append("\n"); 319 } 320 return(str.toString()); 321 322 } 323 324 335 336 341 public static String SQLToUpperCase(String s) 342 { 343 return s.toUpperCase(Locale.ENGLISH); 344 } 345 346 347 352 public static String SQLToLowerCase(String s) 353 { 354 return s.toLowerCase(Locale.ENGLISH); 355 356 } 357 358 359 370 public static boolean SQLEqualsIgnoreCase(String s1, String s2) 371 { 372 if (s2 == null) 373 return false; 374 else 375 return s1.toUpperCase(Locale.ENGLISH).equals(s2.toUpperCase(Locale.ENGLISH)); 376 377 } 378 379 } 380 381 | Popular Tags |