Java String Sub String substringBetween(String str, String open, String close)

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

Description

substring Between

License

Apache License

Declaration

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

Method Source Code

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

public class Main {
    private static final String EMPTY = "";

    public static String substringBetween(String str, String open, String close) {
        if (str == null || open == null || close == null) {
            return null;
        }/* w  w  w . jav  a  2s .c  o m*/
        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 String substring(String str, int start) {
        if (str == null) {
            return null;
        }

        // handle negatives, which means last n characters
        if (start < 0) {
            start = str.length() + start; // remember start is negative
        }

        if (start < 0) {
            start = 0;
        }
        if (start > str.length()) {
            return EMPTY;
        }

        return str.substring(start);
    }
}

Related

  1. substringBetween(String s, String part1, String part2)
  2. substringBetween(String source, String strBegin, String strEnd)
  3. substringBetween(String str, String before, String after)
  4. substringBetween(String str, String open, String close)
  5. substringBetween(String str, String open, String close)
  6. substringBetween(String str, String open, String close, int fromIndex)
  7. substringBetween(String str, String pos1, String pos2)
  8. substringBetween(String str, String tag)
  9. substringBetween(String str, String tag)