Here you can find the source of trunc(String string, int length)
Parameter | Description |
---|---|
string | the string to truncate |
length | The final length of the new string after trucating |
public static String trunc(String string, int length)
//package com.java2s; //License from project: Apache License public class Main { /**/*from w ww. j a v a 2 s .c o m*/ * Truncates the String by removing characters from the right side of the * String so it is length characters long. If the String is already length * characters long or less then the string is returned. If a negative length * is specified the characters are removed from the left side of the string. * * @param string * the string to truncate * @param length * The final length of the new string after trucating * @return the truncated string */ public static String trunc(String string, int length) { int absLength = Math.abs(length); if ((string == null) || (string.length() <= absLength)) { return string; } if (length < 0) { int sLength = string.length(); return string.substring(sLength - absLength, sLength); } return string.substring(0, length); } }