Here you can find the source of truncate(String str, int length)
Parameter | Description |
---|---|
length | the string length, the provided String should be truncated to. If this is below zero, the String isn't truncated at all, and returned without any modification |
str | the String that is to be truncated. If this is <code>null</code>, no operation is performed |
null
if the provided String was null
public static String truncate(String str, int length)
//package com.java2s; /*/*from www .ja va 2 s . c o m*/ * This file is part of ROOSSTER. * Copyright 2004, Benjamin Reitzammer <benjamin@roosster.org> * All rights reserved. * * ROOSSTER is free software; you can redistribute it and/or modify * it under the terms of the Artistic License. * * You should have received a copy of the Artistic License * along with ROOSSTER; if not, go to * http://www.opensource.org/licenses/artistic-license.php for details * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ public class Main { /** * Truncate a String to a specified length * * @param length the string length, the provided String should be * truncated to. If this is below zero, the String isn't truncated * at all, and returned without any modification * @param str the String that is to be truncated. If this is * <code>null</code>, no operation is performed * @return the truncated String, or <code>null</code> if the * provided String was <code>null</code> */ public static String truncate(String str, int length) { if (str == null || length < 0) return str; else return str.length() > length + 1 ? str.substring(0, length) : str; } }