Java String Truncate truncate(String str, int maxSize)

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

Description

Truncate a string to a max size.

License

Open Source License

Parameter

Parameter Description
str the string to possibly truncate
maxSize the maximum size of the string

Return

the truncated string. if str param is null, then the returned string will also be null.

Declaration

public static String truncate(String str, int maxSize) 

Method Source Code

//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

public class Main {
    /**//  w w  w  . j  ava2s. com
     * Truncate a string to a max size.
     * 
     * @param str the string to possibly truncate
     * @param maxSize the maximum size of the string
     * @return the truncated string.  if <code>str</code> param is null, then the returned string will also be null.
     */
    public static String truncate(String str, int maxSize) {
        if (str == null) {
            return null;
        }

        if (str.length() <= maxSize) {
            return str;
        }

        return str.substring(0, maxSize);
    }
}

Related

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