commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephen Colebourne <scolebou...@btopenworld.com>
Subject Re: [lang] StringUtils.interrupt
Date Sun, 05 Feb 2006 00:23:49 GMT
This looks vaguely similar to the methods in WordUtils. Possibly it 
could be done as an adjustment to one of those, although I don't know 
how common a case it is.

Stephen


C. Grobmeier wrote:
> 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: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message