Java String Truncate truncateString(String source, String suffix)

Here you can find the source of truncateString(String source, String suffix)

Description

Returns a new string that is source with suffix removed from the end.

License

Apache License

Declaration

public static String truncateString(String source, String suffix) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from   ww  w .j av  a  2 s.com
       Returns a new string that is <i>source</i> with <i>charCount</i>
       number of characters removed from the end.
       If <i>charCount</i> >= length of <i>source</i> an empty string is returned.
     */
    public static String truncateString(String source, int charCount) {
        int tNewLength = Math.max(0, source.length() - charCount);
        return source.substring(0, tNewLength);
    }

    /**
       Returns a new string that is <i>source</i> with <i>suffix</i>
       removed from the end.
       If <i>suffix</i> is not at the end of <i>source</i>, <i>source</i> is returned.
     */
    public static String truncateString(String source, String suffix) {
        return (source.endsWith(suffix)) ? truncateString(source,
                suffix.length()) : source;
    }
}

Related

  1. truncateString(String errCode, int length, boolean isRight)
  2. truncateString(String inStr, int maxValue)
  3. truncateString(String s)
  4. truncateString(String s, int numChars)
  5. truncateString(String source, int prefixLength, int totalLength)
  6. truncateString(String str, int length)
  7. truncateString(String str, int length, String suffix)
  8. truncateString(String str, int maxlen)
  9. truncateString(String str, int size)