substring Between two substrings - Android java.lang

Android examples for java.lang:String Substring

Description

substring Between two substrings

Demo Code

import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    /**//  w  ww .  j a  va  2s  .  c o  m
     * <pre>
     * StringUtil.substringBetween(null, *, *)       = null
     * StringUtil.substringBetween(*, null, *)       = null
     * StringUtil.substringBetween(*, *, null)       = null
     * StringUtil.substringBetween("h<a>n", "<", ">") = "a"
     * StringUtil.substringBetween("h<a><b>n", "<", ">") = "a"
     * </pre>
     * 
     */
    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);
            } else {
               
            }
        }
        return null;
    }

}

Related Tutorials