Java String Ends With endsWith(CharSequence s, CharSequence seq)

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

Description

Behaves like String.endsWith but for CharSequence.

License

Open Source License

Parameter

Parameter Description
s the string
seq the sequence to test

Return

true if s ends with seq and false otherwise

Declaration

public static boolean endsWith(CharSequence s, CharSequence seq) 

Method Source Code

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

    /**
     * Behaves like <code>String.endsWith</code> and starts search backward from
     * <code>fromIndex</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> ends with <code>seq</code> and
     *         <code>false</code> otherwise
     */
    public static boolean endsWith(CharSequence s, CharSequence seq, int fromIndex) {
        return regionMatches(false, s, s.length() - seq.length() - 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. endsWith(char[] fieldDescriptor, char c)
  2. endsWith(CharSequence cs, CharSequence suffix)
  3. endsWith(CharSequence cs, String postfix)
  4. endsWith(CharSequence s, char c)
  5. endsWith(CharSequence s, char c)
  6. endsWith(CharSequence s, CharSequence sub)
  7. endsWith(CharSequence seq, char... any)
  8. endsWith(CharSequence source, CharSequence search)
  9. endsWith(CharSequence str, char suffix)