Java String Sub String subString(String src, String start, String to)

Here you can find the source of subString(String src, String start, String to)

Description

Example: subString("abcd","a","c")="b"

License

Apache License

Parameter

Parameter Description
src a parameter
start null while start from index=0
to null while to index=src.length

Declaration

public static String subString(String src, String start, String to) 

Method Source Code

//package com.java2s;
/*//from  www.j a va 2  s . co  m
 * Copyright 1999-2101 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Example: subString("abcd","a","c")="b"
     * 
     * @param src
     * @param start null while start from index=0
     * @param to null while to index=src.length
     * @return
     */
    public static String subString(String src, String start, String to) {
        int indexFrom = start == null ? 0 : src.indexOf(start);
        int indexTo = to == null ? src.length() : src.indexOf(to);
        if (indexFrom < 0 || indexTo < 0 || indexFrom > indexTo) {
            return null;
        }

        if (null != start) {
            indexFrom += start.length();
        }

        return src.substring(indexFrom, indexTo);

    }

    /**
     * Example: subString("abcdc","a","c",true)="bcd"
     * 
     * @param src
     * @param start null while start from index=0
     * @param to null while to index=src.length
     * @param toLast true while to index=src.lastIndexOf(to)
     * @return
     */
    public static String subString(String src, String start, String to, boolean toLast) {
        if (!toLast) {
            return subString(src, start, to);
        }
        int indexFrom = start == null ? 0 : src.indexOf(start);
        int indexTo = to == null ? src.length() : src.lastIndexOf(to);
        if (indexFrom < 0 || indexTo < 0 || indexFrom > indexTo) {
            return null;
        }

        if (null != start) {
            indexFrom += start.length();
        }

        return src.substring(indexFrom, indexTo);

    }
}

Related

  1. substring(String src, int beginIndex, int endIndex)
  2. substring(String src, int start_idx, int end_idx)
  3. subString(String src, int startIndex, int endIndex)
  4. subString(String src, String begin, String end)
  5. substring(String src, String fromToken, String toToken)
  6. subString(String src, String start, String to)
  7. subString(String srcStr, int subLen)
  8. substring(String str, char pattern)
  9. substring(String str, int beginIndex)