Java String Sub String subString(String src, int startIndex, int endIndex)

Here you can find the source of subString(String src, int startIndex, int endIndex)

Description

Find substring start by one index end by index

License

Open Source License

Parameter

Parameter Description
src string to be splited
startIndex start index
endIndex end index

Return

string between start and end

Declaration

public static String subString(String src, int startIndex, int endIndex) 

Method Source Code

//package com.java2s;
/*/*ww  w  . j  a  va2 s. c om*/
 * File: $RCSfile$
 *
 * Copyright (c) 2005 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with Wincor Nixdorf.
 */

public class Main {
    /**
     * Find substring start by one string end by another string
     *
     * @param src   source string to be split
     * @param start start string
     * @param end   end string
     * @return string between start and end
     */
    public static String subString(String src, String start, String end) {
        int iStart = src.indexOf(start);
        int iEnd = src.indexOf(end);
        if ((iStart == -1) || (iEnd == -1)) {
            return null;
        }
        iEnd += end.length();
        return src.substring(iStart, iEnd + 1);
    }

    /**
     * Find substring start by one string end by index
     *
     * @param src   source string to be split
     * @param start start string
     * @param end   end index
     * @return string between start and end
     */
    public static String subString(String src, String start, int end) {
        int iStart = src.indexOf(start);
        if ((iStart == -1) || (end == -1)) {
            return null;
        }
        return src.substring(iStart, end);
    }

    /**
     * Find substring start by one index end by index
     *
     * @param src        string to be splited
     * @param startIndex start index
     * @param endIndex   end index
     * @return string between start and end
     */
    public static String subString(String src, int startIndex, int endIndex) {
        if ((startIndex == -1) || (endIndex == -1)) {
            return null;
        }
        return src.substring(startIndex, endIndex);
    }

    /**
     * Find substring start by first index and end by length
     *
     * @param src    source string to be split
     * @param length end index
     * @return string between start and end
     */
    public static String subString(String src, int length) {
        if (src == null) {
            return "";
        }
        if (src.length() <= length) {
            return src;
        } else {
            return src.substring(0, length);
        }
    }
}

Related

  1. substring(String source, int size)
  2. substring(String source, int startIndex)
  3. subString(String source, int startIndex, int count)
  4. substring(String src, int beginIndex, int endIndex)
  5. substring(String src, int start_idx, int end_idx)
  6. subString(String src, String begin, String end)
  7. substring(String src, String fromToken, String toToken)
  8. subString(String src, String start, String to)
  9. subString(String src, String start, String to)