Java String Tokenize substringsBetween(final String str, final String open, final String close, boolean tokenReservedFlag)

Here you can find the source of substringsBetween(final String str, final String open, final String close, boolean tokenReservedFlag)

Description

Searches a String for substrings delimited by a start and end tag, returning all matching substrings in an array.

A null input String returns null .

License

Apache License

Parameter

Parameter Description
str the String containing the substrings, null returns null, empty returns empty
open the String identifying the start of the substring, empty returns null
close the String identifying the end of the substring, empty returns null

Return

a String Array of substrings, or null if no match

Declaration

public static List<String> substringsBetween(final String str, final String open, final String close,
        boolean tokenReservedFlag) 

Method Source Code

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

import java.util.*;

public class Main {
    /**//ww w . ja v  a2s  .  c  o  m
     * <p>Searches a String for substrings delimited by a start and end tag,
     * returning all matching substrings in an array.</p>
     * <p>
     * <p>A {@code null} input String returns {@code null}.
     * A {@code null} open/close returns {@code null} (no match).
     * An empty ("") open/close returns {@code null} (no match).</p>
     * <p>
     * <pre>
     * StringUtils.substringsBetween("[a][b][c]", "[", "]") = ["a","b","c"]
     * StringUtils.substringsBetween(null, *, *)            = null
     * StringUtils.substringsBetween(*, null, *)            = null
     * StringUtils.substringsBetween(*, *, null)            = null
     * StringUtils.substringsBetween("", "[", "]")          = []
     * </pre>
     *
     * @param str   the String containing the substrings, null returns null, empty returns empty
     * @param open  the String identifying the start of the substring, empty returns null
     * @param close the String identifying the end of the substring, empty returns null
     * @return a String Array of substrings, or {@code null} if no match
     * @since 2.3
     */
    public static List<String> substringsBetween(final String str, final String open, final String close,
            boolean tokenReservedFlag) {
        if (str == null || isEmpty(open) || isEmpty(close)) {
            return null;
        }
        final int strLen = str.length();
        if (strLen == 0) {
            return new ArrayList<>();
        }
        final int closeLen = close.length();
        final int openLen = open.length();
        final List<String> list = new ArrayList<String>();
        int pos = 0;
        while (pos < strLen - closeLen) {
            int start = str.indexOf(open, pos);
            if (start < 0) {
                break;
            }
            start += openLen;
            final int end = str.indexOf(close, start);
            if (end < 0) {
                break;
            }
            if (tokenReservedFlag) {
                list.add(str.substring(start - openLen, end + closeLen));
            } else {
                list.add(str.substring(start, end));
            }
            pos = end + closeLen;
        }
        if (list.isEmpty()) {
            return null;
        }
        return list;
    }

    /**
     * <p>
     * Checks if a CharSequence is empty ("") or null.
     * </p>
     * <p>
     * <pre>
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("bob")     = false
     * StringUtils.isEmpty("  bob  ") = false
     * </pre>
     * <p>
     * <p>
     * NOTE: This method changed in Lang version 2.0. It no longer trims the
     * CharSequence. That functionality is available in isBlank().
     * </p>
     *
     * @param cs the CharSequence to check, may be null
     * @return {@code true} if the CharSequence is empty or null
     * @since 3.0 Changed signature from isEmpty(String) to
     * isEmpty(CharSequence)
     */
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }
}

Related

  1. readTokens(String line)
  2. removeEmptyStrings(String[] tokens)
  3. removeMatchingRegex(String regex, String replacement, String[] tokens, boolean removeEmpty)
  4. removeStopWords(String[] tokens, Set stopWords)
  5. stringTokenizer(String in)
  6. tokenArray(StringTokenizer st)
  7. tokenize(final String aInput, final String aDelimiters)
  8. tokenize(final String s)
  9. tokenize(String array, String delimiter)