truncate String by length - Java java.lang

Java examples for java.lang:String Truncate

Description

truncate String by length

Demo Code

public class Main {

  public static void main(String[] argv) {
    String name = "java2s.com this is a test java2s.com this is a test java2s.com this is a test java2s.com this is a test java2s.com this is a test ";
    System.out.println(truncateString(name));
  }//from   www .  ja  v  a2s.  c o m

  private static final int TRUNCATE_LENGTH = 60;

  public static String truncateString(String name) {
    if (name != null && name.length() > TRUNCATE_LENGTH) {
      return name.substring(0, TRUNCATE_LENGTH) + " ...";
    }
    return name;
  }

}

Related Tutorials