Java String Truncate truncate(String message, String substring)

Here you can find the source of truncate(String message, String substring)

Description

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.

License

Open Source License

Parameter

Parameter Description
message An error message or other string that needs to be truncated.
substring A substring to find in message .

Return

If message contains substring then the return value consists of the portion of message up to and including substring , followed by "...". Otherwise the return value is message .

Declaration

public static String truncate(String message, String substring) 

Method Source Code

//package com.java2s;

public class Main {
    /**//ww w  . j a  va  2s.com
     * 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.
     * @param message              An error message or other string that
     *                             needs to be truncated.
     * @param substring            A substring to find in {@code message}.
     * @return                     If {@code message} contains
     *                             {@code substring} then the return value
     *                             consists of the portion of {@code message}
     *                             up to and including {@code substring},
     *                             followed by "...".  Otherwise the return
     *                             value is {@code message}.
     */
    public static String truncate(String message, String substring) {
        int i = message.indexOf(substring);
        if (i < 0) {
            return message;
        }
        return message.substring(0, i + substring.length()) + "...";
    }
}

Related

  1. truncate(String input, int maxLength)
  2. truncate(String input, int maxLength)
  3. truncate(String input, int size)
  4. truncate(String message)
  5. truncate(String message, int length, boolean includeCount)
  6. truncate(String original, int number)
  7. truncate(String original, String ellipsis, int maxLength)
  8. truncate(String originalString, int size)
  9. truncate(String s)