Java ASCII asciiEndsWithIgnoreCase(String source, String suffix)

Here you can find the source of asciiEndsWithIgnoreCase(String source, String suffix)

Description

Returns whether the given source string ends with the suffix, ignoring case and assuming that the strings are ascii encoded.

License

Apache License

Parameter

Parameter Description
source the string to match.
suffix the suffix to test.

Return

true if the source does end with the given suffix, or false if not.

Declaration

public static boolean asciiEndsWithIgnoreCase(String source, String suffix) 

Method Source Code

//package com.java2s;
/*/*from w  ww.  j a va2s.c om*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 {
    /**
     * Returns whether the given source string ends with the suffix, ignoring
     * case and assuming that the strings are ascii encoded.
     *
     * @param source
     *            the string to match.
     * @param suffix
     *            the suffix to test.
     * @return {@code true} if the source does end with the given suffix, or
     *         {@code false} if not.
     */
    public static boolean asciiEndsWithIgnoreCase(String source, String suffix) {
        int length = suffix.length();
        if (length > source.length()) {
            return false;
        }
        int offset = source.length() - length;
        for (int i = 0; i < length; i++) {
            char c1 = source.charAt(i + offset);
            char c2 = suffix.charAt(i);
            if (c1 != c2 && toASCIIUpperCase(c1) != toASCIIUpperCase(c2)) {
                return false;
            }
        }
        return true;
    }

    private static final byte toASCIIUpperCase(byte b) {
        if ('a' <= b && b <= 'z') {
            return (byte) (b - ('a' - 'A'));
        }
        return b;
    }

    private static final char toASCIIUpperCase(char c) {
        if ('a' <= c && c <= 'z') {
            return (char) (c - ('a' - 'A'));
        }
        return c;
    }
}

Related

  1. asciiChar(int value)
  2. asciiChars()
  3. asciiCharToBytes(char[] chars)
  4. asciiDump(byte[] in)
  5. asciiEbcdic(String source)
  6. asciiEqualsIgnoreCase(byte[] a, byte[] b)
  7. asciiEqualsIgnoreCase(byte[] buf1, byte[] buf2)
  8. asciiFill2(String str, int startIdx, String fillStr)
  9. asciify(String s)