Java String Truncate truncateStringChars(String strIn, String substituteChars, int maxChars, boolean keepRightSide)

Here you can find the source of truncateStringChars(String strIn, String substituteChars, int maxChars, boolean keepRightSide)

Description

Truncates a string to the number of characters in maxChars The character count includes the substitute characters (which will only be added if the string is truncated)

License

Open Source License

Declaration

public static String truncateStringChars(String strIn, String substituteChars, int maxChars,
        boolean keepRightSide) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   w  ww .j  a  va  2s .  c om*/
     * Truncates a string to the number of characters in maxChars
     * The character count includes the substitute characters (which will only be added if the string is truncated)
     */
    public static String truncateStringChars(String strIn, String substituteChars, int maxChars,
            boolean keepRightSide) {
        if (strIn.length() <= maxChars) {
            return strIn;
        }
        strIn = strIn.replaceAll(" ", "");
        if (strIn.length() <= maxChars) {
            return strIn;
        }
        if (keepRightSide) {
            strIn = substituteChars
                    + strIn.substring(strIn.length() - (maxChars - substituteChars.length()), strIn.length());
        } else {
            strIn = strIn.substring(0, maxChars - substituteChars.length()) + substituteChars;
        }
        return strIn;
    }
}

Related

  1. truncateString(String str, int size)
  2. truncateString(String str, int toLen)
  3. truncateString(String string, int maxLength)
  4. truncateString(String text, int truncateAt)
  5. truncateStringAt(String source, int len)
  6. truncateStringToUtf8(final String original, final int maxBytes)
  7. truncateSubjectText(String subject)
  8. truncateTestName(String test)
  9. truncateText(String str, int lower, int upper, String appendToEnd)