Java String Truncate truncateToLength(String inputString, int truncateLength)

Here you can find the source of truncateToLength(String inputString, int truncateLength)

Description

Method which returns a truncated version of inputString.

License

Open Source License

Parameter

Parameter Description
inputString The String to be truncated
truncateLength The Length of the resulting Truncated String

Declaration

public static String truncateToLength(String inputString, int truncateLength) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from ww  w.ja v  a  2  s  .  c  o  m
     * Method which returns a truncated version of inputString.
     * If truncateLength is > String length, then the String is returned unmodified
     * @param inputString The String to be truncated
     * @param truncateLength The Length of the resulting Truncated String
     * @return
     */
    public static String truncateToLength(String inputString, int truncateLength) {
        try {

            if (truncateLength > inputString.length() - 1)
                return inputString;
            else
                return inputString.substring(0, truncateLength);

        } catch (Exception e) {

            if (inputString == null)
                return " ";
            else
                return inputString;
        }
    }
}

Related

  1. truncateTestName(String test)
  2. truncateText(String str, int lower, int upper, String appendToEnd)
  3. truncateText(String text, int maxCharLength)
  4. truncateTime(String dateString)
  5. truncateToFirstLine(String string)
  6. truncateToMaxLen(String input, int len)
  7. truncateType(String s)
  8. truncateType(String type)
  9. truncateUri(String uri)