Here you can find the source of truncateStringChars(String strIn, String substituteChars, int maxChars, boolean keepRightSide)
public static String truncateStringChars(String strIn, String substituteChars, int maxChars, boolean keepRightSide)
//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; } }