Java String substring by marker

Introduction

Gets the String that is nested in between two instances of the same String.

getNestedString(null, *)            = null
getNestedString("", "")             = ""
getNestedString("", "tag")          = null
getNestedString("tagabctag", null)  = null
getNestedString("tagabctag", "")    = ""
getNestedString("tagabctag", "tag") = "abc"
public class Main {
  public static void main(String[] argv) throws Exception {
    String str = "sitedemo2s.comsite";
    String tag = "site";
    System.out.println(getNestedString(str, tag));
  }/*from ww w  .jav a  2 s  .c  o m*/
  public static final String EMPTY = "";
  public static String getNestedString(String str, String tag) {
    return substringBetween(str, tag, tag);
  }

  /**
   * <p>
   * Gets the String that is nested in between two Strings. Only the first match
   * is returned.
   * </p>
   *
   * <p>
   * A <code>null</code> input String returns <code>null</code>. A
   * <code>null</code> open/close returns <code>null</code> (no match). An empty
   * ("") open/close returns an empty string.
   * </p>
   *
   * <pre>
   * getNestedString(null, *, *)          = null
   * getNestedString("", "", "")          = ""
   * getNestedString("", "", "tag")       = null
   * getNestedString("", "tag", "tag")    = null
   * getNestedString("yabcz", null, null) = null
   * getNestedString("yabcz", "", "")     = ""
   * getNestedString("yabcz", "y", "z")   = "abc"
   * getNestedString("yabczyabcz", "y", "z")   = "abc"
   * </pre>
   *
   * @param str
   *          the String containing nested-string, may be null
   * @param open
   *          the String before nested-string, may be null
   * @param close
   *          the String after nested-string, may be null
   * @return the nested String, <code>null</code> if no match
   * @deprecated Use the better named
   *             {@link #substringBetween(String, String, String)}. Method will be
   *             removed in Commons Lang 3.0.
   */
  public static String getNestedString(String str, String open, String close) {
    return substringBetween(str, open, close);
  }

  /**
   * <p>
   * Gets the String that is nested in between two instances of the same String.
   * </p>
   *
   * <p>
   * A <code>null</code> input String returns <code>null</code>. A
   * <code>null</code> tag returns <code>null</code>.
   * </p>
   *
   * <pre>
   * substringBetween(null, *)            = null
   * substringBetween("", "")             = ""
   * substringBetween("", "tag")          = null
   * substringBetween("tagabctag", null)  = null
   * substringBetween("tagabctag", "")    = ""
   * substringBetween("tagabctag", "tag") = "abc"
   * </pre>
   *
   * @param str
   *          the String containing the substring, may be null
   * @param tag
   *          the String before and after the substring, may be null
   * @return the substring, <code>null</code> if no match
   * @since 2.0
   */
  public static String substringBetween(String str, String tag) {
    return substringBetween(str, tag, tag);
  }

  /**
   * <p>
   * Gets the String that is nested in between two Strings. Only the first match
   * is returned.
   * </p>
   *
   * <p>
   * A <code>null</code> input String returns <code>null</code>. A
   * <code>null</code> open/close returns <code>null</code> (no match). An empty
   * ("") open and close returns an empty string.
   * </p>
   *
   * <pre>
   * substringBetween("wx[b]yz", "[", "]") = "b"
   * substringBetween(null, *, *)          = null
   * substringBetween(*, null, *)          = null
   * substringBetween(*, *, null)          = null
   * substringBetween("", "", "")          = ""
   * substringBetween("", "", "]")         = null
   * substringBetween("", "[", "]")        = null
   * substringBetween("yabcz", "", "")     = ""
   * substringBetween("yabcz", "y", "z")   = "abc"
   * substringBetween("yabczyabcz", "y", "z")   = "abc"
   * </pre>
   *
   * @param str
   *          the String containing the substring, may be null
   * @param open
   *          the String before the substring, may be null
   * @param close
   *          the String after the substring, may be null
   * @return the substring, <code>null</code> if no match
   * @since 2.0
   */
  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;
  }

}

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//package com.demo2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String src = "demo2s.com";
        String separator = ".";
        System.out.println(after(src, separator));
    }//from  ww w.ja  v a 2s . c  o  m

    /**
     * Returns the part of the input string <em>after</em> the Separator.
     * Should the separator not be found, the unchanged string is returned.
     * @param src the string to process
     * @param separator the separator sequencue
     * @return the result of the processing
     */
    public static String after(String src, String separator) {
        int pos = src.indexOf(separator);

        if (pos < 1)
            return src;
        else
            return src.substring(pos + separator.length());
    }

}



PreviousNext

Related