CharSequence ends With - Java java.lang

Java examples for java.lang:CharSequence

Description

CharSequence ends With

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        CharSequence string = "java2s.com";
        CharSequence endsWith = "java2s.com";
        System.out.println(endsWith(string, endsWith));
    }//from   www  . j ava 2s  . c o  m

    public static boolean endsWith(CharSequence string,
            CharSequence endsWith) {
        if ((string == null) || (endsWith == null)) {
            return false;
        }

        if (string.length() < endsWith.length()) {
            return false;
        }

        if (endsWith.length() == 0) {
            return false;
        }

        if (endsWith.length() == 1) {
            return string.charAt(string.length() - 1) == endsWith.charAt(0);
        }

        int max = endsWith.length();
        for (int i = 0; i < max - 1; i++) {
            int endString = string.length() - i - 1;
            int endWithString = endsWith.length() - i - 1;
            if (string.charAt(endString) != endsWith.charAt(endWithString)) {
                return false;
            }
        }
        return true;
    }
}

Related Tutorials