Here you can find the source of truncate(String str, int maxSize)
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. |
maxSize
characters if necessary
public static String truncate(String str, int maxSize)
//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; } } }