Android String Sub String Get substringBetween(String str, String open, String close)

Here you can find the source of substringBetween(String str, String open, String close)

Description

Gets the String that is nested in between two Strings.

License

Apache License

Parameter

Parameter Description
str the String containing the substring, may be null
open the String before the substring, may be null
close the String after the substring, may be null

Return

the substring, null if no match

Declaration

public static String substringBetween(String str, String open,
        String close) 

Method Source Code

//package com.java2s;
/*/*  w  w w  .j a  v  a 2s. com*/
 * (C) Copyright Itude Mobile B.V., The Netherlands
 * 
 * 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 {
    /**
     * Gets the String that is nested in between two Strings.
     * Only the first match is returned.
     *
     * A <code>null</code> input String returns <code>null</code>.
     * A <code>null</code> open/close returns <code>null</code> (no match).
     * An empty ("") open and close returns an empty string.
     *
     * <pre>
     * StringUtils.substringBetween("w[ie]be", "[", "]") = "ie"
     * StringUtils.substringBetween(null, *, *)          = null
     * StringUtils.substringBetween(*, null, *)          = null
     * StringUtils.substringBetween(*, *, null)          = null
     * StringUtils.substringBetween("", "", "")          = ""
     * StringUtils.substringBetween("", "", "]")         = null
     * StringUtils.substringBetween("", "[", "]")        = null
     * StringUtils.substringBetween("wiebe", "", "")     = ""
     * StringUtils.substringBetween("wiebe", "w", "e")   = "ieb"
     * StringUtils.substringBetween("wiebewiebe", "w", "w")   = "ieb"
     * </pre>
     *
     * @param str  the String containing the substring, may be null
     * @param open  the String before the substring, may be null
     * @param close  the String after the substring, may be null
     * @return the substring, <code>null</code> if no match
     * @since 2.0
     */
    public static String substringBetween(String str, String open,
            String close) {
        if (str == null || open == null || close == null) {
            return null;
        }
        int start = str.indexOf(open);
        if (start != -1) {
            int end = str.indexOf(close, start + open.length());
            if (end != -1) {
                return str.substring(start + open.length(), end);
            }
        }
        return null;
    }
}

Related

  1. substringBeforeLast(String str, String separator)
  2. unicodePreservingSubstring(String paramString, int paramInt1, int paramInt2)
  3. unicodePreservingSubstring(String str, int begin, int end)
  4. unicodePreservingSubstring(String str, int begin)
  5. substringAfter(String str, String separator)
  6. subStringEndString(String sourceStr, String endString)
  7. hightLinghtString(String destStr, String subStr, int color)
  8. replace(String source, String subject, String object)
  9. replaceOnce(String source, String subject, String object)