Java String Truncate truncate(String str, int maxSize)

Here you can find the source of truncate(String str, int maxSize)

Description

truncate

License

Apache License

Parameter

Parameter Description
str the string to truncate (may be null of empty)
maxSize the maximum size the result string should have. must be greater than zero.

Return

a new string truncated a maxSize characters if necessary

Declaration

public static String truncate(String str, int maxSize) 

Method Source Code

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

public class Main {
    /**/*from  w ww . j  a v  a  2 s  .  c  o m*/
     * @param str     the string to truncate (may be null of empty)
     * @param maxSize the maximum size the result string should have. must be greater than zero.
     * @return a new string truncated a <code>maxSize</code> characters if necessary
     */
    public static String truncate(String str, int maxSize) {
        if (str != null && maxSize >= 0 && str.length() > maxSize) {
            return str.substring(0, maxSize);
        } else {
            return str;
        }
    }
}

Related

  1. truncate(String str, int length)
  2. truncate(String str, int max)
  3. truncate(String str, int max)
  4. truncate(String str, int maxLen)
  5. truncate(String str, int maxSize)
  6. truncate(String str, int n)
  7. truncate(String str, int size, boolean useEllipsis)
  8. truncate(String str, int start, int end)
  9. truncate(String str, String prefix, String suffix)