public static String tryToTrimToSize ( String s , int length , boolean trimSuffix ) { if ( s == null || s . isEmpty ( ) ) { return s ; } else if ( s . length ( ) <= length ) { return s ; } else if ( s . contains ( File . separator ) ) { String before = s . substring ( 0 , s . lastIndexOf ( File . separator ) ) ; String after = s . substring ( s . lastIndexOf ( File . separator ) ) ; if ( ! trimSuffix && length - after . length ( ) > 4 ) { return Strings . tryToTrimToSize ( before , length - after . length ( ) , true ) + after ; } else { return Strings . tryToTrimToSize ( before , length - 3 , true ) + File . separator + ".." ; } } else { return ".." ; } }
Try to trim a String to the given size .