Java Utililty Methods String Truncate

List of utility methods to do String Truncate

Description

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

Method

Stringtruncate(String input, int size)
truncates a String to the specified length
if (input != null) {
    int len = input.length();
    if (len <= size) {
        return input;
    } else {
        return input.substring(0, size) + "...";
} else {
...
Stringtruncate(String message)
truncate
if (message.lastIndexOf(".") < 500 && message.lastIndexOf(".") != -1) {
    return message.substring(0, message.lastIndexOf("."));
message = message.substring(0, 495);
if (message.lastIndexOf(".") < 500 && message.lastIndexOf(".") != -1) {
    return message.substring(0, message.lastIndexOf("."));
return message + "...";
...
Stringtruncate(String message, int length, boolean includeCount)
truncate
if (message == null) {
    return null;
if (message.length() <= length) {
    return message;
String result = message.substring(0, length) + "...";
if (includeCount) {
...
Stringtruncate(String message, String substring)
This method is typically used to remove system-specific text from error messages, so that the error messages can be checked in a system-independent fashion.
int i = message.indexOf(substring);
if (i < 0) {
    return message;
return message.substring(0, i + substring.length()) + "...";
Stringtruncate(String original, int number)

Keeps the 'number' first characters, and might be useful to keep your user's privacy :
 truncate("Hernandez", 3)
will produce: "Her"

If number is bigger than the size of the String, the whole string is kept :
 truncate("Hernandez", 24)
will produce: "Hernandez"

If number==0, the function returns an empty string.

if (number < 0) {
    throw new IllegalArgumentException("number :" + number + " is <0");
if (original == null) {
    throw new IllegalArgumentException("original string is null");
String newLastName = original.length() >= number ? original.substring(0, number) : original;
return newLastName;
...
Stringtruncate(String original, String ellipsis, int maxLength)
Truncates a String , and if necessary adds an ellipsis to the end of the string.
if (original.length() > maxLength) {
    if (ellipsis == null) {
        ellipsis = "...";
    return original.substring(0, maxLength - ellipsis.length()) + ellipsis;
return original;
Stringtruncate(String originalString, int size)
If the size of the original string is larger than the passed in size, this method will remove the vowels from the original string.
if (originalString.length() <= size) {
    return originalString;
String vowels = "AaEeIiOoUu";
StringBuilder newStringBufferTmp = new StringBuilder(originalString.length());
int counter = originalString.length() - size;
for (int index = (originalString.length() - 1); index >= 0; index--) {
    if (vowels.indexOf(originalString.charAt(index)) == -1) {
...
Stringtruncate(String s)
truncate
return truncate(s, MAX_CONTENT_LENGTH);
Stringtruncate(String s)
truncate
return truncate(s, 15);
Stringtruncate(String s, int begin, int end)
truncate
if (isNullOrNone(s)) {
    return "";
if (begin < 0) {
    return "";
if (begin >= end) {
    return "";
...