Here you can find the source of substringBetween(final String s, final String tag)
Gets the String that is nested in between two instances of the same String.
public static String substringBetween(final String s, final String tag)
//package com.java2s; /**/*www . ja v a2 s .c o m*/ * Copyright Sangram Jadhav. All rights reserved. * * 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. */ public class Main { /** * <p> * Gets the String that is nested in between two instances of the same * String. * </p> * * <p> * A {@code null} input String returns {@code null}. A {@code null} tag * returns {@code null}. * </p> * */ public static String substringBetween(final String s, final String tag) { return substringBetween(s, tag, tag); } /** * <p> * Gets the String that is nested in between two Strings. Only the first * match is returned. * </p> * * <p> * A {@code null} input String returns {@code null}. A {@code null} * open/close returns {@code null} (no match). An empty ("") open and close * returns an empty string. * </p> * */ public static String substringBetween(final String s, final String open, final String close) { if (s == null || open == null || close == null) { return null; } final int start = s.indexOf(open); if (start >= 0) { final int end = s.indexOf(close, start + open.length()); if (end >= 0) { return s.substring(start + open.length(), end); } } return null; } /** * Finds first occurrence of a substring in the given source but within limited range [start, end). * It is fastest possible code, but still original <code>String.indexOf(String, int)</code> * is much faster (since it uses char[] value directly) and should be used when no range is needed. * * @param s source string for examination * @param substr substring to find * @param startIndex starting index * @param endIndex ending index * @return index of founded substring or -1 if substring not found */ public static int indexOf(String s, String substr, int startIndex, int endIndex) { if (startIndex < 0) { startIndex = 0; } int srclen = s.length(); if (endIndex > srclen) { endIndex = srclen; } int sublen = substr.length(); if (sublen == 0) { return startIndex > srclen ? srclen : startIndex; } int total = endIndex - sublen + 1; char c = substr.charAt(0); mainloop: for (int i = startIndex; i < total; i++) { if (s.charAt(i) != c) { continue; } int j = 1; int k = i + 1; while (j < sublen) { if (substr.charAt(j) != s.charAt(k)) { continue mainloop; } j++; k++; } return i; } return -1; } /** * Finds the first occurrence of a character in the given source but within limited range (start, end]. */ public static int indexOf(String s, char c, int startIndex, int endIndex) { if (startIndex < 0) { startIndex = 0; } int srclen = s.length(); if (endIndex > srclen) { endIndex = srclen; } for (int i = startIndex; i < endIndex; i++) { if (s.charAt(i) == c) { return i; } } return -1; } /** * Finds the very first index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> if * noting found. * * @param s source string * @param arr string array */ public static int[] indexOf(String s, String arr[]) { return indexOf(s, arr, 0); } /** * Finds the very first index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> * if noting found. * * @param s source string * @param arr string array * @param start starting position */ public static int[] indexOf(String s, String arr[], int start) { int arrLen = arr.length; int index = Integer.MAX_VALUE; int last = -1; for (int j = 0; j < arrLen; j++) { int i = s.indexOf(arr[j], start); if (i != -1) { if (i < index) { index = i; last = j; } } } return last == -1 ? null : new int[] { last, index }; } /** * Finds the very first index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> if * noting found. * * @param s source string * @param c char array */ public static int[] indexOf(String s, char c[]) { return indexOf(s, c, 0); } /** * Finds the very first index of a char from the specified array. It * returns an int[2] where int[0] represents the char index and int[1] * represents position where char was found. Returns <code>null</code> * if noting found. * * @param s source string * @param c char array * @param start starting position */ public static int[] indexOf(String s, char c[], int start) { int arrLen = c.length; int index = Integer.MAX_VALUE; int last = -1; for (int j = 0; j < arrLen; j++) { int i = s.indexOf(c[j], start); if (i != -1) { if (i < index) { index = i; last = j; } } } return last == -1 ? null : new int[] { last, index }; } /** * <p> * Gets a substring from the specified String avoiding exceptions. * </p> * * <p> * A negative start position can be used to start {@code n} characters from * the end of the String. * </p> * * <p> * A {@code null} String will return {@code null}. An empty ("") String will * return "". * </p> * */ public static String substring(final String s, int start) { if (s == null) { return null; } // handle negatives, which means last n characters if (start < 0) { start = s.length() + start; // remember start is negative } if (start < 0) { start = 0; } if (start > s.length()) { return ""; } return s.substring(start); } /** * <p> * Gets a substring from the specified String avoiding exceptions. * </p> * * <p> * A negative start position can be used to start/end {@code n} characters * from the end of the String. * </p> * * <p> * The returned substring starts with the character in the {@code start} * position and ends before the {@code end} position. All position counting * is zero-based -- i.e., to start at the beginning of the string use * {@code start = 0}. Negative start and end positions can be used to * specify offsets relative to the end of the String. * </p> * * <p> * If {@code start} is not strictly to the left of {@code end}, "" is * returned. * </p> * */ public static String substring(final String s, int start, int end) { if (s == null) { return null; } // handle negatives if (end < 0) { end = s.length() + end; // remember end is negative } if (start < 0) { start = s.length() + start; // remember start is negative } // check length next if (end > s.length()) { end = s.length(); } // if start is greater than end, return "" if (start > end) { return ""; } if (start < 0) { start = 0; } if (end < 0) { end = 0; } return s.substring(start, end); } }