Java String Match regionMatches(boolean ignoreCase, CharSequence thisSeq, int toffset, CharSequence otherSeq, int ooffset, int len)

Here you can find the source of regionMatches(boolean ignoreCase, CharSequence thisSeq, int toffset, CharSequence otherSeq, int ooffset, int len)

Description

region Matches

License

Open Source License

Declaration

public static boolean regionMatches(boolean ignoreCase, CharSequence thisSeq, int toffset,
            CharSequence otherSeq, int ooffset, int len) 

Method Source Code

//package com.java2s;
/*//from www  . ja  v a 2  s . c o  m
 * 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 {
    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. regionMatches(byte[] ba1, int pos1, byte[] ba2, int pos2, int len)
  2. regionMatches(byte[] subValue, byte[] superValue, int offset)
  3. regionMatches(char[] source, char[] target, int sIndex)
  4. regionMatches(CharSequence a, int i, CharSequence b, int j, int len)