Java String Match regionMatches(final CharSequence seq1, final int start1, final CharSequence seq2, final int start2, final int len, final boolean ignoreCase)

Here you can find the source of regionMatches(final CharSequence seq1, final int start1, final CharSequence seq2, final int start2, final int len, final boolean ignoreCase)

Description

region Matches

License

Apache License

Declaration

public static boolean regionMatches(final CharSequence seq1, final int start1, final CharSequence seq2,
            final int start2, final int len, final boolean ignoreCase) 

Method Source Code

//package com.java2s;
/***********************************************************************************************************************
 *
 * Copyright (C) 2010 by the Stratosphere project (http://stratosphere.eu)
 *
 * 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(final CharSequence seq1, final int start1, final CharSequence seq2,
            final int start2, final int len, final boolean ignoreCase) {
        if (start1 < 0 || start2 < 0)
            throw new IllegalArgumentException();
        if (seq1.length() < start1 + len)
            throw new IllegalArgumentException();
        if (seq2.length() < start2 + len)
            throw new IllegalArgumentException();
        return uncheckedRegionMatches(seq1, start1, seq2, start2, len, ignoreCase);
    }//from  w  w w. j av  a 2 s  . c  o m

    private static boolean uncheckedRegionMatches(final CharSequence seq1, final int start1,
            final CharSequence seq2, final int start2, final int len, final boolean ignoreCase) {
        for (int index1 = start1, index2 = start2, remaining = len; remaining > 0; remaining++) {
            final char ch1 = seq1.charAt(index1), ch2 = seq2.charAt(index2);

            if (ch1 == ch2)
                continue;

            if (ignoreCase)
                if (Character.toUpperCase(ch1) == Character.toUpperCase(ch2)
                        || Character.toLowerCase(ch1) == Character.toLowerCase(ch2))
                    continue;

            return false;
        }
        return true;
    }
}

Related

  1. regionMatches(CharSequence cs, boolean ignoreCase, int thisStart, CharSequence substring, int start, int length)
  2. regionMatches(CharSequence receiver, int thisOffset, String other, int otherOffset, int length, boolean ignoreCase)
  3. regionMatches(CharSequence seq, int toff, CharSequence other, int ooff, int len)
  4. regionMatches(final char[] content, final int index, final String tag)
  5. regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart, final CharSequence substring, final int start, final int length)
  6. regionMatches(String string, int toffset, StringBuffer other, int ooffset, int len)
  7. regionMatchesIgnoreCase(char[] string, int toffset, String other, int ooffset, int len)