Java String Starts Wtih startsWith(CharSequence str, CharSequence prefix)

Here you can find the source of startsWith(CharSequence str, CharSequence prefix)

Description

Return true if one CharSequence starts with the other.

License

Open Source License

Declaration

public static boolean startsWith(CharSequence str, CharSequence prefix) 

Method Source Code

//package com.java2s;
/*//from   ww w  .j  av  a  2  s . co  m
StringUtils.java : A stand alone utility class.
Copyright (C) 2000 Justin P. McCarthy
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
    
To further contact the author please email jpmccar@gjt.org
    
    
Modifications, copyright 2001-2014 Tuma Solutions, LLC; distributed under the
LGPL, as described above.
    
*/

public class Main {
    /** Return true if one CharSequence starts with the other.
     * 
     * This performs the equivalent of String.startsWith, but accepts arbitrary
     * CharSequence objects.  (Thus, it works for StringBuffer arguments.)
     */
    public static boolean startsWith(CharSequence str, CharSequence prefix) {
        return startsWith(str, prefix, 0);
    }

    /** Return true if one CharSequence starts with the other, starting at a
     * particular point in the string.
     * 
     * This performs the equivalent of String.startsWith, but accepts arbitrary
     * CharSequence objects.  (Thus, it works for StringBuffer arguments.)
     */
    public static boolean startsWith(CharSequence str, CharSequence prefix, int offset) {
        // guard against null arguments
        if (str == null || prefix == null)
            return false;

        // quick optimization for pure string args
        if (str instanceof String && prefix instanceof String)
            return ((String) str).startsWith((String) prefix, offset);

        // quick test to see if lengths make a "startsWith" impossible
        if (str.length() < prefix.length() + offset)
            return false;

        // walk over the characters in question, looking for mismatches.
        int j = prefix.length() + offset;
        for (int i = prefix.length(); i-- > 0;)
            if (str.charAt(--j) != prefix.charAt(i))
                return false;

        // must be a match.
        return true;
    }
}

Related

  1. startsWith(CharSequence s, CharSequence seq)
  2. startsWith(CharSequence seq, char... any)
  3. startsWith(CharSequence seq, String str)
  4. startsWith(CharSequence source, CharSequence search)
  5. startsWith(CharSequence str, char prefix)
  6. startsWith(final boolean caseSensitive, final char[] text, final char[] prefix)
  7. startsWith(final CharSequence str, final CharSequence prefix)
  8. startsWith(final CharSequence target, final CharSequence prefix)
  9. startsWith(final E[] array, final E[] prefix)