Java String Ends With endsWith(String s, char c)

Here you can find the source of endsWith(String s, char c)

Description

An efficient method for checking if a string ends with a character.

License

Apache License

Parameter

Parameter Description
s The string to check. Can be <jk>null</jk>.
c The character to check for.

Return

true if the specified string is not null and ends with the specified character.

Declaration

public static boolean endsWith(String s, char c) 

Method Source Code

//package com.java2s;
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *

public class Main {
    /**//from   w  w  w  . ja va  2s. c o m
     * An efficient method for checking if a string ends with a character.
     *
     * @param s The string to check.  Can be <jk>null</jk>.
     * @param c The character to check for.
     * @return <jk>true</jk> if the specified string is not <jk>null</jk> and ends with the specified character.
     */
    public static boolean endsWith(String s, char c) {
        if (s != null) {
            int i = s.length();
            if (i > 0)
                return s.charAt(i - 1) == c;
        }
        return false;
    }

    /**
     * Returns the character at the specified index in the string without throwing exceptions.
     *
     * @param s The string.
     * @param i The index position.
     * @return The character at the specified index, or <code>0</code> if the index is out-of-range or the string
     *    is <jk>null</jk>.
     */
    public static char charAt(String s, int i) {
        if (s == null)
            return 0;
        if (i < 0 || i >= s.length())
            return 0;
        return s.charAt(i);
    }
}

Related

  1. endsWith(String fullString, String subString)
  2. endsWith(String haystack, String needle)
  3. endsWith(String in, String str)
  4. endsWith(String inEnd, String inValue)
  5. endsWith(String receiver, String... needles)
  6. endsWith(String s, String end)
  7. endsWith(String s, String end)
  8. endsWith(String s, String end)
  9. endsWith(String s, String ending)