Here you can find the source of substr(String s, String sub, boolean before)
private static String substr(String s, String sub, boolean before)
//package com.java2s; /*/*from w ww .j a va 2 s. c om*/ * Copyright (c) 2013 Game Salutes. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v3 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl.html * * Contributors: * Game Salutes - Repackaging and modifications of original work under University of Chicago and Apache License 2.0 shown below * * Repackaging from edu.uchicago.nsit.iteco.utils to com.gamesalutes.utils * * Copyright 2008 - 2011 University of Chicago * * 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 { private static String substr(String s, String sub, boolean before) { if (s == null) throw new NullPointerException("s"); if (sub == null) throw new NullPointerException("sub"); int index = indexOf(s, sub); if (index == -1) return null; int sublen = sub.length(); if (before) { return index > 0 ? s.substring(0, index) : ""; } else { return index + sublen < s.length() ? s.substring(index + sublen) : ""; } } /** * Behaves like <code>String.indexOf</code> but for <code>CharSequence</code>. * * @param s the string * @param seq the sequence to test * @return the index of <code>seq</code> in <code>s</code> or <code>-1</code> if not found */ public static int indexOf(CharSequence s, CharSequence seq) { return indexOf(s, seq, 0, true); } /** * Behaves like <code>String.indexOf</code> but for <code>CharSequence</code>. * * @param s the string * @param seq the sequence to test * @param fromIndex the index in <code>s</code> to begin the search * @return the index of <code>seq</code> in <code>s</code> or <code>-1</code> if not found */ public static int indexOf(CharSequence s, CharSequence seq, int fromIndex) { return indexOf(s, seq, fromIndex, true); } /** * Behaves like <code>String.indexOf</code> but for <code>CharSequence</code>. * * @param s the string * @param c the character to test * @return the index of <code>c</code> in <code>s</code> or <code>-1</code> if not found */ public static int indexOf(CharSequence s, char c) { return indexOf(s, c, 0, true); } /** * Behaves like <code>String.indexOf</code> but for <code>CharSequence</code>. * * @param s the string * @param c the character to test * @param fromIndex the index in <code>s</code> to begin the search * @return the index of <code>c</code> in <code>s</code> or <code>-1</code> if not found */ public static int indexOf(CharSequence s, char c, int fromIndex) { fromIndex = normalizeIndex(s, fromIndex, true); if (fromIndex < 0) return -1; return indexOf(s, c, fromIndex, true); } private static int indexOf(CharSequence s, char c, int index, boolean fromStart) { int len = s.length(); if (fromStart) { for (int i = index; i < len; ++i) if (s.charAt(i) == c) return i; } else { for (int i = index; i >= 0; --i) if (s.charAt(i) == c) return i; } return -1; } private static int indexOf(CharSequence s, CharSequence seq, int i, boolean fromStart) { // do a regional compare starting at begining of string if (s == null) throw new NullPointerException("s"); if (seq == null) throw new NullPointerException("seq"); int lenTotal = s.length(); int lenSeq = seq.length(); if (lenSeq > lenTotal) return -1; // s always contains empty string if (lenSeq == 0) return 0; // behave like String.indexOf and String.lastIndexOf if (fromStart) { if (i < 0) i = 0; else if (i >= lenTotal) return -1; } else { if (i >= lenTotal) i = lenTotal - 1; else if (i < 0) return -1; } // look for a match in first character int start = i; char c = seq.charAt(0); while (fromStart ? (start < lenTotal) : (start >= 0)) { int index = indexOf(s, c, start, true); // no match if (index == -1) return -1; // first character matches: examine rest of sequence if (regionMatches(false, s, index, seq, 0, lenSeq)) return index; start = index + (fromStart ? 1 : -1); } return -1; } public static int indexOf(boolean ignoreCase, CharSequence thisSeq, int toffset, char c, int len) { int to = toffset; // Note: toffset, ooffset, or len might be near -1>>>1. if ((toffset < 0) || (toffset > (long) thisSeq.length() - len)) { return -1; } if (!ignoreCase) { while (len-- > 0) { char c1 = thisSeq.charAt(to); if (c1 == c) { return to; } ++to; } } else { char uc = Character.toUpperCase(c); char lc = Character.toLowerCase(c); while (len-- > 0) { char c1 = thisSeq.charAt(to); if (c1 == c) { return to; } // If characters don't match but case may be ignored, // try converting both characters to uppercase. // If the results match, then the comparison scan should // continue. char u1 = Character.toUpperCase(c1); if (u1 == uc) { return to; } // Unfortunately, conversion to uppercase does not work properly // for the Georgian alphabet, which has strange rules about case // conversion. So we need to make one last check before // exiting. if (Character.toLowerCase(u1) == lc) { return to; } ++to; } } return -1; } private static int normalizeIndex(CharSequence s, int i, boolean fromStart) { int len = s.length(); // behave like String.indexOf and String.lastIndexOf if (fromStart) { if (i < 0) i = 0; else if (i >= len) return -1; } else { if (i >= len) i = len - 1; else if (i < 0) return -1; } return i; } public static boolean regionMatches(boolean ignoreCase, CharSequence thisSeq, int toffset, CharSequence otherSeq, int ooffset, int len) { int to = toffset; int po = ooffset; // Note: toffset, ooffset, or len might be near -1>>>1. if ((ooffset < 0) || (toffset < 0) || (toffset > (long) thisSeq.length() - len) || (ooffset > (long) otherSeq.length() - len)) { return false; } while (len-- > 0) { char c1 = thisSeq.charAt(to++); char c2 = otherSeq.charAt(po++); if (c1 == c2) { continue; } if (ignoreCase) { // If characters don't match but case may be ignored, // try converting both characters to uppercase. // If the results match, then the comparison scan should // continue. char u1 = Character.toUpperCase(c1); char u2 = Character.toUpperCase(c2); if (u1 == u2) { continue; } // Unfortunately, conversion to uppercase does not work properly // for the Georgian alphabet, which has strange rules about case // conversion. So we need to make one last check before // exiting. if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) { continue; } } // if return false; } // while return true; } }