public String trimToSize(String str,int maxStringLength){
  if (str == null)   return null;
  if (str.length() > maxStringLength) {
    StringBuilder sb=new StringBuilder(str);
    sb.setLength(maxStringLength);
    str=sb.toString();
  }
  return str;
}

public String trimToSize ( String str , int maxStringLength ) { if ( str == null ) return null ; if ( str . length ( ) > maxStringLength ) { StringBuilder sb = new StringBuilder ( str ) ; sb . setLength ( maxStringLength ) ; str = sb . toString ( ) ; } return str ; }
trim a given string to maxstringlength if it is over maxstringlength .
