Hi, Could someone look at this and makes sure it makes sense. I am gradually trying to aggregate all the utility code I use in various projects into excalibur. If you can think of better name for class, package or any of the methods feel free to mod them. If there is any extra code you could add then feel free to do so ;) About the only other bit of string manip code I have to add is word wrapping but my version of that is ... err somewhat messy ... so if anyone wants to add their own version ... ;) On Thu, 9 Aug 2001 18:15, [EMAIL PROTECTED] wrote: > donaldp 01/08/09 01:15:37 > > Added: src/scratchpad/org/apache/avalon/excalibur/util > StringUtil.java > Log: > A first sweep at general string utility methods. Basically this is an > aggregation of code from a number of different projects. > > Revision Changes Path > 1.1 > jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/util/St >ringUtil.java > > Index: StringUtil.java > =================================================================== > /* > * Copyright (C) The Apache Software Foundation. All rights reserved. > * > * This software is published under the terms of the Apache Software > License * version 1.1, a copy of which has been included with this > distribution in * the LICENSE file. > */ > package org.apache.avalon.excalibur.util; > > /** > * A set of utility operations that work on or create strings. > * > * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a> > * @version CVS $Revision: 1.1 $ $Date: 2001/08/09 08:15:37 $ > */ > public final class StringUtil > { > > private final static String SPACE_16 = " > "; private final static String SPACE_8 = " "; > private final static String SPACE_4 = " "; private final > static String SPACE_2 = " "; > private final static String SPACE_1 = " "; > > ///Private Constructor to block instantiation > private StringUtil() > { > } > > /** > * Replace substrings of one string with another string and return > altered string. * > * @param original input string > * @param oldString the substring section to replace > * @param newString the new substring replacing old substring section > * @return converted string > */ > public static String replaceSubString( final String original, > final String oldString, > final String newString ) > { > final StringBuffer sb = new StringBuffer(); > > int end = original.indexOf( oldString ); > int start = 0; > final int stringSize = oldString.length(); > > while( end != -1 ) > { > sb.append( original.substring( start, end ) ); > sb.append( newString ); > start = end + stringSize; > end = original.indexOf( oldString, start ); > } > > end = original.length(); > sb.append( original.substring( start, end ) ); > > return sb.toString(); > } > > /** > * Display bytes in hex format. > * <p>The display puts hex display on left and then writes out > * textual representation on right. The text replaces any > * non-printing character with a '.'</p> > * > * <p>Note that this code was based on work done by Barry Peterson > * on the Q2Java project</p> > * > * @param data the bytes to display > * @param length the number of bytes to display per line > * @return The display string > */ > public final static String hexDisplay( final byte[] data, final int > length ) { > final StringBuffer sb = new StringBuffer(); > > for( int i = 0; i < length; i += 16 ) > { > //int lineEnd = Math.min( i+16, fArrayLength ); > int lineSize = Math.min( 16, length - i ); > final int lineEnd = lineSize + i; > > for( int j = i; j < lineEnd; j++) > { > final int value = data[ j ] & 0xFF; > > if( value < 16 ) sb.append( '0' ); > sb.append( Integer.toHexString( value ) ); > sb.append(' '); > } > > int padcount = 16 - lineSize; > while( padcount > 0 ) > { > padcount--; > sb.append( " " ); > } > > sb.append(" "); > > for( int j = i; j < lineEnd; j++ ) > { > final int value = data[ j ] & 0xFF; > > //Shouldn't 255 be lower???????? > if( (value < 32) || (value > 255) ) sb.append( '.' ); > else sb.append( (char)value ); > } > > sb.append( '\n' ); > } > > return sb.toString(); > } > > /** > * Truncate a string to maximum length; > * > * @param string the string > * @param length the length > * @return a truncated string or original string if it is short > enough */ > public static String truncate( final String string, final int length > ) { > if( length >= string.length() ) return string; > else return string.substring( 0, length ); > } > > /** > * Truncate a string in a nice manner. > * The method will attempt to truncate the string > * on whitespace and append "..." to the end. > * > * @author <a href="mailto:[EMAIL PROTECTED]">Nate Sammons</a> > */ > public static String truncateNicely( final String string, > final int length ) > { > if( 3 >= length ) > { > final StringBuffer sb = new StringBuffer( length ); > for( int i = 0; i < length; i++ ) sb.append( '.' ); > return sb.toString(); > } > > final StringBuffer result = new StringBuffer(); > > final int sublen = length - 3; > final StringTokenizer st = new StringTokenizer( string ); > while( st.hasMoreTokens() ) > { > final String word = st.nextToken(); > final int currentLength = result.length(); > > if( word.length() + currentLength + 1 <= sublen ) > { > if( 0 != currentLength ) result.append( ' ' ); > result.append( word ); > } > else > { > if( 0 == result.length() ) > { > //Theres no whitespace in string so just cut it > result.append( string.substring( 0, sublen ) ); > } > result.append( "..." ); > break; > } > } > > return result.toString(); > } > > /** > * Splits the string on every token into an array of stack frames. > * > * @param string the string > * @param onToken the token > * @return the resultant array > */ > public static String[] splitString( final String string, final String > onToken ) { > final StringTokenizer tokenizer = new StringTokenizer( string, > onToken ); > > final ArrayList lines = new ArrayList(); > > while( tokenizer.hasMoreTokens() ) > { > lines.add( tokenizer.nextToken() ); > } > > return (String[])lines.toArray( new String[ 0 ] ); > } > > /** > * Utility to format a string given a set of constraints. > * TODO: Think of a better name than format!!!! ;) > * > * @param minSize the minimum size of output (0 to ignore) > * @param maxSize the maximum size of output (0 to ignore) > * @param rightJustify true if the string is to be right justified in > it's box. * @param string the input string > */ > public static String format( final int minSize, > final int maxSize, > final boolean rightJustify, > final String string ) > { > final StringBuffer sb = new StringBuffer( maxSize ); > format( sb, minSize, maxSize, rightJustify, string ); > return sb.toString(); > } > > /** > * Utility to format a string given a set of constraints. > * TODO: Think of a better name than format!!!! ;) > * Note this was thieved from the logkit project. > * > * @param sb the StringBuffer > * @param minSize the minimum size of output (0 to ignore) > * @param maxSize the maximum size of output (0 to ignore) > * @param rightJustify true if the string is to be right justified in > it's box. * @param string the input string > */ > public static void format( final StringBuffer sb, > final int minSize, > final int maxSize, > final boolean rightJustify, > final String string ) > { > final int size = string.length(); > > if( size < minSize ) > { > //assert( minSize > 0 ); > if( rightJustify ) > { > appendWhiteSpace( sb, minSize - size ); > sb.append( string ); > } > else > { > sb.append( string ); > appendWhiteSpace( sb, minSize - size ); > } > } > else if( maxSize > 0 && maxSize < size ) > { > if( rightJustify ) > { > sb.append( string.substring( size - maxSize ) ); > } > else > { > sb.append( string.substring( 0, maxSize ) ); > } > } > else > { > sb.append( string ); > } > } > > /** > * Append a certain number of whitespace characters to a > StringBuffer. * > * @param sb the StringBuffer > * @param length the number of spaces to append > */ > public static void appendWhiteSpace( final StringBuffer sb, int > length ) { > while( length >= 16 ) > { > sb.append( SPACE_16 ); > length -= 16; > } > > if( length >= 8 ) > { > sb.append( SPACE_8 ); > length -= 8; > } > > if( length >= 4 ) > { > sb.append( SPACE_4 ); > length -= 4; > } > > if( length >= 2 ) > { > sb.append( SPACE_2 ); > length -= 2; > } > > if( length >= 1 ) > { > sb.append( SPACE_1 ); > length -= 1; > } > } > } > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] -- Cheers, Pete *-----------------------------------------------------* | "Faced with the choice between changing one's mind, | | and proving that there is no need to do so - almost | | everyone gets busy on the proof." | | - John Kenneth Galbraith | *-----------------------------------------------------* --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
