truncate String - Java java.lang

Java examples for java.lang:String Truncate

Introduction

The following code shows how to truncate String

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String name = "java2s.com";
        System.out.println(truncateString(name));
    }//from  w w  w  .j  a v  a2 s  .  c om

    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