Java String Ends With endsWith(CharSequence source, CharSequence search)

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

Description

Returns true if the source sequence ends with the search sequence.

License

Apache License

Declaration

public static boolean endsWith(CharSequence source, CharSequence search) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    /**//from www . ja v  a  2s.c  o  m
     *  Returns true if the source sequence ends with the search sequence. This is a
     *  call to {@link #containsAt} with calculated location.
     */
    public static boolean endsWith(CharSequence source, CharSequence search) {
        if (source == null)
            return false;
        if (search == null)
            return false;

        int loc = source.length() - search.length();
        return containsAt(source, search, loc);
    }

    /**
     *  Returns true of the passed source sequence contains the specified search
     *  sequence starting at the given location. Will return false if either the
     *  source or search sequences are null, or the specified location is outside
     *  of the source sequence (either high or low).
     */
    public static boolean containsAt(CharSequence source, CharSequence search, int loc) {
        if (source == null)
            return false;
        if (search == null)
            return false;

        if (loc < 0)
            return false;
        if (loc + search.length() > source.length())
            return false;

        for (int ii = 0; ii < search.length(); ii++) {
            if (source.charAt(loc + ii) != search.charAt(ii))
                return false;
        }

        return true;
    }
}

Related

  1. endsWith(CharSequence s, char c)
  2. endsWith(CharSequence s, char c)
  3. endsWith(CharSequence s, CharSequence seq)
  4. endsWith(CharSequence s, CharSequence sub)
  5. endsWith(CharSequence seq, char... any)
  6. endsWith(CharSequence str, char suffix)
  7. endsWith(CharSequence str, CharSequence suffix)
  8. endsWith(final boolean caseSensitive, final String text, final String suffix)
  9. endsWith(final byte[] big, final byte[] suffix)