Java String Starts Wtih startsWith(CharSequence s, CharSequence seq)

Here you can find the source of startsWith(CharSequence s, CharSequence seq)

Description

Behaves like String.startsWith but for CharSequence.

License

Open Source License

Parameter

Parameter Description
s the string
seq the sequence to test

Return

true if s starts with seq and false otherwise

Declaration

public static boolean startsWith(CharSequence s, CharSequence seq) 

Method Source Code

//package com.java2s;
/*//from  ww  w . j a  va2s .  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 {
    /**
     * Behaves like <code>String.startsWith</code> but for <code>CharSequence</code>.
     * 
     * @param s the string
     * @param seq the sequence to test
     * @return <code>true</code> if <code>s</code> starts with <code>seq</code>  and
     *         <code>false</code> otherwise
     */
    public static boolean startsWith(CharSequence s, CharSequence seq) {
        return regionMatches(false, s, 0, seq, 0, seq.length());
    }

    /**
     * Behaves like <code>String.startsWith</code> but for <code>CharSequence</code>.
     * 
     * @param s the string
     * @param seq the sequence to test
     * @param fromIndex index in <code>s</code> to begin search
     * @return <code>true</code> if <code>s</code> starts with <code>seq</code> from <code>fromIndex</code> 
     *         and <code>false</code> otherwise
     */
    public static boolean startsWith(CharSequence s, CharSequence seq, int fromIndex) {
        return regionMatches(false, s, fromIndex, seq, 0, seq.length());
    }

    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;
    }
}

Related

  1. startsWith(CharSequence cs, CharSequence prefix, boolean caseSensitive)
  2. startsWith(CharSequence input, String prefix)
  3. startsWith(CharSequence seq, char... any)
  4. startsWith(CharSequence seq, String str)
  5. startsWith(CharSequence source, CharSequence search)
  6. startsWith(CharSequence str, char prefix)