Java String Truncate truncate(String x, int length)

Here you can find the source of truncate(String x, int length)

Description

String Truncator if a null or empty string is passed, an empty string is returned

License

Mozilla Public License

Parameter

Parameter Description
x a parameter
length a parameter

Declaration

public static String truncate(String x, int length) 

Method Source Code

//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;
    }
}

Related

  1. truncate(String value, int length)
  2. truncate(String value, int length)
  3. truncate(String value, int length)
  4. truncate(String value, int length)
  5. truncate(String value, int maxLen, String ellipses)
  6. truncateAndTrailOffText(String text, int limit)
  7. truncateAndTrim0(String str, String delim)
  8. truncateAt(final String input, final String... substrings)
  9. truncateAt(String str, char chr)