Java String Truncate truncate(String str, int start, int end)

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

Description

Returns the string truncated from the start index to end index.

License

Open Source License

Parameter

Parameter Description
str a parameter
start The beginning index, inclusive.
end The ending index, exclusive.

Declaration

public static String truncate(String str, int start, int end) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (C) Devamatre Technologies 2009
 * //w ww.jav  a 2 s .  c  o  m
 * This code is licensed to Devamatre under one or more contributor license 
 * agreements. The reproduction, transmission or use of this code or the 
 * snippet is not permitted without prior express written consent of Devamatre. 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the license is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied and the 
 * offenders will be liable for any damages. All rights, including  but not
 * limited to rights created by patent grant or registration of a utility model 
 * or design, are reserved. Technical specifications and features are binding 
 * only insofar as they are specifically and expressly agreed upon in a written 
 * contract.
 * 
 * You may obtain a copy of the License for more details at:
 *      http://www.devamatre.com/licenses/license.txt.
 *      
 * Devamatre reserves the right to modify the technical specifications and or 
 * features without any prior notice.
 *****************************************************************************/

public class Main {
    /**
     * Returns the string truncated from the start index to end index.
     * 
     * @param str
     * @param start
     *            The beginning index, inclusive.
     * @param end
     *            The ending index, exclusive.
     * @return
     */
    public static String truncate(String str, int start, int end) {
        if (isNullOrEmpty(str)) {
            throw new IllegalArgumentException("Invalid String!, str: " + str);
        }

        if (start < 0 || start > end || end > str.length()) {
            throw new StringIndexOutOfBoundsException("Invalid Index!, start: " + start + ", endIndex:" + end);
        }

        StringBuilder sBuilder = new StringBuilder(str);
        sBuilder.delete(start, end);
        return sBuilder.toString();
    }

    /**
     * Checks whether the given string is null or empty.
     * 
     * @param str
     * @return
     */
    public static boolean isNullOrEmpty(String str) {
        return str == null || str.isEmpty();
    }
}

Related

  1. truncate(String str, int maxLen)
  2. truncate(String str, int maxSize)
  3. truncate(String str, int maxSize)
  4. truncate(String str, int n)
  5. truncate(String str, int size, boolean useEllipsis)
  6. truncate(String str, String prefix, String suffix)
  7. truncate(String string, final int length)
  8. truncate(String string, int length)
  9. truncate(String string, int maxLength, boolean atBeginning, String ellipse)