Here you can find the source of truncate(String x, int length)
Parameter | Description |
---|---|
x | a parameter |
length | a parameter |
public static String truncate(String x, int length)
//package com.java2s; /**//from w ww . j ava 2 s .c o m * This Source Code Form is subject to the terms of the Mozilla Public License, * v. 2.0. If a copy of the MPL was not distributed with this file, You can * obtain one at http://mozilla.org/MPL/2.0/. * * If it is not possible or desirable to put the notice in a particular file, * then You may include the notice in a location (such as a LICENSE file in a * relevant directory) where a recipient would be likely to look for such a * notice. * * */ public class Main { /** * String Truncator if a null or empty string is passed, an empty string is * returned * * @param x * @param length * @return */ public static String truncate(String x, int length) { if (x == null || x.length() == 0) { return ""; } String ret = x; if (x.length() > length) { ret = x.substring(0, length); } return ret; } }