Get substring Between open and close tag - Java java.lang

Java examples for java.lang:String Substring

Description

Get substring Between open and close tag

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String str = "java2s.com";
        String tag = "o";
        System.out.println(substringBetween(str, tag));
    }//from  w w  w. jav a2 s.co  m

    public static final String EMPTY_STRING = "";

    public static String substringBetween(String str, String tag) {
        return substringBetween(str, tag, tag, 0);
    }

    public static String substringBetween(String str, String open,
            String close) {
        return substringBetween(str, open, close, 0);
    }

    public static String substringBetween(String str, String open,
            String close, int fromIndex) {
        if ((str == null) || (open == null) || (close == null)) {
            return null;
        }

        int start = str.indexOf(open, fromIndex);

        if (start != -1) {
            int end = str.indexOf(close, start + open.length());

            if (end != -1) {
                return str.substring(start + open.length(), end);
            }
        }

        return null;
    }

}

Related Tutorials