Hello all,

building my websites guestbook, i figured out that somebody entered a big string (exactly: an URL). This blew up my complete layout. HTML doesn't give any solution on this, you simply can't break up this stupid long string.

Looking at commons lang i found a lot of join/split-operations, but it seems that never anybody needed something of a "separate after 20 characters with a blank"-function. Well, i am not sure if ever somebody REALLY needs this besides me, but you'll never know. So i decided to let take you a look at my method (attached).

OK, i know it's not the very best method in the world (at least it midnight and i was at a whiskey tasting this evening), but if somebody thinks it could be useful to be included in org.apache.commons.lang.StringUtils i will improve it and would be glad to contribute "interrupt()" to you. If somebody has a better idea than this, your comments are welcome.

Let me know.
Having said this, good night.

- Chris

/**
 * Interrupts the string text after the max number of characters 
 * with the given seperator. Before this operation is beeing done, a split
 * operation with the seperator is beeing fullfilled. This means that seperator
 * is beeing trimmed from the beginning and the end of the string.
 *  
 * @param text - the String which has to be interrupted
 * @param seperator - the seperator to interrupt with
 * @param max - maximal occurencies of other characters before the string is 
beeing interrupted
 * @return the interrupted string, null if the text is null
 */
public static String interrupt(String text, String seperator, int max) {
        if(text==null) {
                return null;
        }
        if(seperator == null) {
                seperator = " ";
        }
        
        String[] array = StringUtils.split(text, seperator);
        StringBuffer result = new StringBuffer();
        
        for(int i=0; i < array.length; i++) {
                String value = array[i];
                if(i > 0) {
                        result.append(seperator);
                }
                
                int length = value.length();
                if(length > max ) {
                        int pieces = length / max;
                        float mod = length % max;
                        if(mod>0) {
                                pieces++;
                        }
                        int c = 0;
                        while( c < pieces ) {
                                if(c>0) {
                                        result.append(seperator);
                                }
                                result.append(StringUtils.mid(value, c*max, 
max));
                                c++;
                        }
                } else {
                        result.append(value);
                }
        }
        return result.toString();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to