Java String Sub String substringBetween(String str, String tag)

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

Description

substring Between

License

Apache License

Declaration

public static String substringBetween(String str, String tag) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String substringBetween(String str, String tag) {
        return substringBetween(str, tag, tag);
    }/*  w w  w .j  a  v a2s. c om*/

    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;
    }

    public static int indexOf(String str, char searchChar) {
        if (isEmpty(str)) {
            return -1;
        }
        return str.indexOf(searchChar);
    }

    public static int indexOf(String str, char searchChar, int startPos) {
        if (isEmpty(str)) {
            return -1;
        }
        return str.indexOf(searchChar, startPos);
    }

    public static int indexOf(String str, String searchStr) {
        if ((str == null) || (searchStr == null)) {
            return -1;
        }
        return str.indexOf(searchStr);
    }

    public static int indexOf(String str, String searchStr, int startPos) {
        if ((str == null) || (searchStr == null)) {
            return -1;
        }
        if ((searchStr.length() == 0) && (startPos >= str.length())) {
            return str.length();
        }
        return str.indexOf(searchStr, startPos);
    }

    public static String substring(String str, int start) {
        if (str == null) {
            return null;
        }
        if (start < 0) {
            start = str.length() + start;
        }
        if (start < 0) {
            start = 0;
        }
        if (start > str.length()) {
            return "";
        }
        return str.substring(start);
    }

    public static String substring(String str, int start, int end) {
        if (str == null) {
            return null;
        }
        if (end < 0) {
            end = str.length() + end;
        }
        if (start < 0) {
            start = str.length() + start;
        }
        if (end > str.length()) {
            end = str.length();
        }
        if (start > end) {
            return "";
        }
        if (start < 0) {
            start = 0;
        }
        if (end < 0) {
            end = 0;
        }
        return str.substring(start, end);
    }

    public static boolean isEmpty(String str) {
        return (str == null) || (str.length() == 0);
    }
}

Related

  1. substringBetween(String str, String open, String close)
  2. substringBetween(String str, String open, String close)
  3. substringBetween(String str, String open, String close, int fromIndex)
  4. substringBetween(String str, String pos1, String pos2)
  5. substringBetween(String str, String tag)
  6. substringBetweenStrings(String within, String pre, String post)
  7. subStringByByte(String orignal, int subcount)
  8. subStringByByte(String str, int byteLenth)
  9. substringByByteCount(String str, int byteCount)