1 8 package org.apache.avalon.excalibur.util; 9 10 import java.util.ArrayList ; 11 import java.util.StringTokenizer ; 12 13 19 public final class StringUtil 20 { 21 24 public final static String LINE_SEPARATOR = System.getProperty( "line.separator" ); 25 26 private final static String SPACE_16 = " "; 27 private final static String SPACE_8 = " "; 28 private final static String SPACE_4 = " "; 29 private final static String SPACE_2 = " "; 30 private final static String SPACE_1 = " "; 31 32 private StringUtil() 34 { 35 } 36 37 40 public final static String concat( final String first, 41 final String second, 42 final String third ) 43 { 44 return new StringBuffer (first).append(second).append(third).toString(); 45 } 46 47 50 public final static String concat( final String [] list ) 51 { 52 final StringBuffer buffer = new StringBuffer (); 53 54 for (int i = 0; i < list.length; i++) 55 { 56 buffer.append(list[i]); 57 } 58 59 return buffer.toString(); 60 } 61 62 70 public final static String replaceSubString( final String original, 71 final String oldString, 72 final String newString ) 73 { 74 final StringBuffer sb = new StringBuffer (); 75 76 int end = original.indexOf( oldString ); 77 int start = 0; 78 final int stringSize = oldString.length(); 79 80 while( end != -1 ) 81 { 82 sb.append( original.substring( start, end ) ); 83 sb.append( newString ); 84 start = end + stringSize; 85 end = original.indexOf( oldString, start ); 86 } 87 88 end = original.length(); 89 sb.append( original.substring( start, end ) ); 90 91 return sb.toString(); 92 } 93 94 107 public final static String hexDisplay( final byte[] data, final int length ) 108 { 109 final StringBuffer sb = new StringBuffer (); 110 111 for( int i = 0; i < length; i += 16 ) 112 { 113 int lineSize = Math.min( 16, length - i ); 115 final int lineEnd = lineSize + i; 116 117 for( int j = i; j < lineEnd; j++) 118 { 119 final int value = data[ j ] & 0xFF; 120 121 if( value < 16 ) sb.append( '0' ); 122 sb.append( Integer.toHexString( value ) ); 123 sb.append( ' ' ); 124 } 125 126 int padcount = 16 - lineSize; 127 while( padcount > 0 ) 128 { 129 padcount--; 130 sb.append( " " ); 131 } 132 133 sb.append( " " ); 134 135 for( int j = i; j < lineEnd; j++ ) 136 { 137 final int value = data[ j ] & 0xFF; 138 139 if( (value < 32) || (value > 255) ) sb.append( '.' ); 141 else sb.append( (char)value ); 142 } 143 144 sb.append( '\n' ); 145 } 146 147 return sb.toString(); 148 } 149 150 157 public final static String truncate( final String string, final int length ) 158 { 159 if( length >= string.length() ) return string; 160 else return string.substring( 0, length ); 161 } 162 163 170 public final static String truncateNicely( final String string, final int length ) 171 { 172 if( length >= string.length() ) return string; 173 else if( 3 >= length ) 174 { 175 final StringBuffer sb = new StringBuffer ( length ); 176 for( int i = 0; i < length; i++ ) sb.append( '.' ); 177 return sb.toString(); 178 } 179 180 final StringBuffer sb = new StringBuffer ( length ); 181 getIndexOfBreak( sb, string, 0, length - 3, true ); 182 sb.append( "..." ); 183 184 return sb.toString(); 185 } 186 187 197 public final static String wordWrap( final String string, final int column, final boolean force ) 198 { 199 final int length = string.length(); 200 final StringBuffer sb = new StringBuffer (); 201 202 int start = 0; 203 int end = getIndexOfBreak( sb, string, start, column, force ); 204 205 while( length != end ) 206 { 207 sb.append( "\n" ); 209 210 start = end; 211 212 end = getIndexOfBreak( sb, string, start, column, force ); 213 } 214 215 return sb.toString(); 216 } 217 218 225 public final static String [] split( final String string, final String onToken ) 226 { 227 final StringTokenizer tokenizer = new StringTokenizer ( string, onToken ); 228 final String [] result = new String [ tokenizer.countTokens() ]; 229 230 for( int i = 0; i < result.length; i++ ) 231 { 232 result[ i ] = tokenizer.nextToken(); 233 } 234 235 return result; 236 } 237 238 245 public final static String join( final String [] strings, final String separator ) 246 { 247 final StringBuffer sb = new StringBuffer (); 248 for( int i = 0; i < strings.length; i++ ) 249 { 250 sb.append( strings[ i ] ); 251 sb.append( separator ); 252 } 253 254 return sb.toString(); 255 } 256 257 266 public final static String format( final int minSize, 267 final int maxSize, 268 final boolean rightJustify, 269 final String string ) 270 { 271 final StringBuffer sb = new StringBuffer ( maxSize ); 272 format( sb, minSize, maxSize, rightJustify, string ); 273 return sb.toString(); 274 } 275 276 287 public final static void format( final StringBuffer sb, 288 final int minSize, 289 final int maxSize, 290 final boolean rightJustify, 291 final String string ) 292 { 293 final int size = string.length(); 294 295 if( size < minSize ) 296 { 297 if( rightJustify ) 299 { 300 appendWhiteSpace( sb, minSize - size ); 301 sb.append( string ); 302 } 303 else 304 { 305 sb.append( string ); 306 appendWhiteSpace( sb, minSize - size ); 307 } 308 } 309 else if( maxSize > 0 && maxSize < size ) 310 { 311 if( rightJustify ) 312 { 313 sb.append( string.substring( size - maxSize ) ); 314 } 315 else 316 { 317 sb.append( string.substring( 0, maxSize ) ); 318 } 319 } 320 else 321 { 322 sb.append( string ); 323 } 324 } 325 326 332 public final static void appendWhiteSpace( final StringBuffer sb, int length ) 333 { 334 while( length >= 16 ) 335 { 336 sb.append( SPACE_16 ); 337 length -= 16; 338 } 339 340 if( length >= 8 ) 341 { 342 sb.append( SPACE_8 ); 343 length -= 8; 344 } 345 346 if( length >= 4 ) 347 { 348 sb.append( SPACE_4 ); 349 length -= 4; 350 } 351 352 if( length >= 2 ) 353 { 354 sb.append( SPACE_2 ); 355 length -= 2; 356 } 357 358 if( length >= 1 ) 359 { 360 sb.append( SPACE_1 ); 361 length -= 1; 362 } 363 } 364 365 374 private final static int getIndexOfBreak( final StringBuffer sb, 375 final String string, 376 final int start, 377 final int maxCount, 378 final boolean forceBreak ) 379 { 380 381 final int end = string.length() - start; 382 int max = Math.min( maxCount, end ); 383 384 int base = 0; 385 for( int i = 0; i < max; i++ ) 386 { 387 final int index = start + i; 388 final char ch = string.charAt( index ); 389 if( !Character.isWhitespace( ch ) ) break; 390 391 base = i + 1; 392 } 393 394 max += base; 395 396 int breakIndex = -1; 397 for( int i = base; i < max; i++ ) 398 { 399 final int index = start + i; 400 final char ch = string.charAt( index ); 401 402 if( Character.isWhitespace( ch ) ) breakIndex = index; 403 } 404 405 if( -1 != breakIndex ) 406 { 407 final String part = string.substring( start + base, breakIndex ); 408 sb.append( part ); 409 return breakIndex + 1; 410 } 411 else 412 { 413 if( forceBreak ) 414 { 415 final String part = string.substring( start + base, start + max ); 416 sb.append( part ); 417 418 return start + max; 419 } 420 else 421 { 422 return getIndexOfBreak( sb, string, start, Integer.MAX_VALUE, true ); 423 } 424 } 425 } 426 } 427 | Popular Tags |