Find substring start by one string end by another string - Android java.lang

Android examples for java.lang:String Substring

Description

Find substring start by one string end by another string

Demo Code

import java.io.UnsupportedEncodingException;
import java.util.*;

public class Main{

    /**// ww w. j av a  2 s  . c o m
     * 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);
    }
    /**
     * 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 Tutorials