Java Utililty Methods String Shorten

List of utility methods to do String Shorten

Description

The list of methods to do String Shorten are organized into topic(s).

Method

StringshortenStringsByRemovingVowelsToFit(String s1, String s2, int maximumStringLength)
Returns a String which is a concatenation of two string which have had enough vowels removed from them so that the sum of the sized of the two strings is less than or equal to the specified size.
int size = s1.length() + s2.length();
if (size <= maximumStringLength) {
    return s1 + s2;
int s1Size = s1.length();
int s2Size = s2.length();
StringBuilder buf1 = new StringBuilder();
StringBuilder buf2 = new StringBuilder();
...
StringshortenTagString(String longTag)
return short (single letter) form of the tag, covers only nouns, verbs, adjectives, and adverbs
longTag = longTag.toLowerCase();
if (longTag.startsWith("n"))
    return "n";
else if (longTag.startsWith("v"))
    return "v";
else if (longTag.startsWith("adj") || longTag.startsWith("j"))
    return "a";
else if (longTag.startsWith("adv") || longTag.startsWith("r"))
...
StringshortenText(final String text, final int maxLength, final boolean addDots)
Returns shortened text.
return text.length() <= maxLength ? text
        : text.substring(0, maxLength > 3 && addDots ? maxLength - 3 : maxLength) + (addDots ? "..." : "");
StringshortenText(int maxWidth, String textValue)
Shorten the given text t so that its length doesn't exceed the given width.
int length = textValue.length();
if (length < maxWidth)
    return textValue;
String ellipsis = "..."; 
int subStrLen = (maxWidth - ellipsis.length()) / 2;
int addtl = (maxWidth - ellipsis.length()) % 2;
StringBuffer sb = new StringBuffer();
sb.append(textValue.substring(0, subStrLen));
...
StringshortenText(String originalText, int maxlength)
Shortens a text to a specific length and appends "..."
if (originalText == null)
    return "";
if (originalText.length() <= maxlength)
    return originalText;
int j = maxlength;
while (originalText.charAt(j--) != ' ')
    ;
StringBuffer text = new StringBuffer();
...
StringshortenText(String string, int threshold)
Shortens the given string to be no longer than the given threshold.
int length = string.length();
if (length > threshold) {
    int chomp = length - threshold + 3;
    int begin = Math.round(threshold / 2) - 1;
    return string.replaceAll(string.substring(begin, begin + chomp), "..."); 
return string;
StringshortenTo(final String value, final int maxLength)
Shortens the given String to no longer than the given length.
return value.substring(0, Math.min(value.length(), maxLength));
StringshortenTo(String string, int lenght)
shorten To
if (string.length() > lenght) {
    string = string.substring(0, lenght - 4) + " ...";
return string;
StringshortenToFirstLast(String filename)
shorten To First Last
if ((filename == null) || (filename.length() < 2)) {
    return filename;
final int firstIndex = filename.indexOf('/', 1);
final int lastIndex = filename.lastIndexOf('/');
if ((firstIndex < 0) || (firstIndex == lastIndex)) {
    return filename;
String result = filename.substring(0, firstIndex) + filename.substring(lastIndex);
return result;
StringshortenType(String typeText)
shorten Type
if (typeText == null)
    return "";
final int i = typeText.lastIndexOf(".");
if (i != -1) {
    return typeText.substring(i + 1);
return typeText;