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

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

Description

Evaluates if the first CharSequence ends with the second.

License

Open Source License

Parameter

Parameter Description
s The search space.
sub The substring to evaluate.

Return

True when s ends with sub or both values are null.

Declaration

public static boolean endsWith(CharSequence s, CharSequence sub) 

Method Source Code

//package com.java2s;
/*//w ww. j a v  a 2s . c o  m
Copyright (c) 1996-2013 Ariba, Inc.
All rights reserved. Patents pending.
    
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.
    
$Id: //ariba/platform/util/core/ariba/util/core/StringUtil.java#41 $
*/

public class Main {
    /**
     * Evaluates if the first CharSequence ends with the second. If both
     * strings are null, then the substring is considered to be a substring
     * of the search string.
     * @param s The search space.
     * @param sub The substring to evaluate.
     * @return True when s ends with sub or both values are null.
     */
    public static boolean endsWith(CharSequence s, CharSequence sub) {
        // special-case nulls, (null, null) should be true
        if (null == s) {
            return null == sub;
        } else if (null == sub) {
            return false;
        }

        int l1 = s.length();
        int l2 = sub.length();
        // .toString required, because .equals on CharSequence doesn't
        // behave as expected
        return l1 >= l2 && s.subSequence(l1 - l2, l1).equals(sub.toString());
    }
}

Related

  1. endsWith(CharSequence cs, CharSequence suffix)
  2. endsWith(CharSequence cs, String postfix)
  3. endsWith(CharSequence s, char c)
  4. endsWith(CharSequence s, char c)
  5. endsWith(CharSequence s, CharSequence seq)
  6. endsWith(CharSequence seq, char... any)
  7. endsWith(CharSequence source, CharSequence search)
  8. endsWith(CharSequence str, char suffix)
  9. endsWith(CharSequence str, CharSequence suffix)