Here you can find the source of truncate(final String str, final int len)
Parameter | Description |
---|---|
str | String to truncate |
len | maximum desired length of new string |
public static String truncate(final String str, final int len)
//package com.java2s; //License from project: Open Source License public class Main { /**//from ww w.j a v a 2s.co m * truncate a string if it longer than the argument * * @param str String to truncate * @param len maximum desired length of new string */ public static String truncate(final String str, final int len) { if (str == null) { throw new NullPointerException("String is null"); } if (len < 0) { throw new IndexOutOfBoundsException("Length is less than zero"); } if (str.length() > len) { return str.substring(0, len); } return str.trim(); } }