Java examples for java.lang:String End
Case insensitive check if a String ends with a specified suffix. nulls are handled without exceptions.
/*//w ww . j a v a2s. c om * Copyright 2013 Guidewire Software, Inc. */ /** * This class is based, in part, on org.apache.commons.lang.StringUtils and is intended * to break the dependency on that project. * * @author <a href="http://jakarta.apache.org/turbine/">Apache Jakarta Turbine</a> * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> * @author Daniel L. Rall * @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a> * @author <a href="mailto:ed@apache.org">Ed Korthof</a> * @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a> * @author Stephen Colebourne * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a> * @author Holger Krauth * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a> * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> * @author Arun Mammen Thomas * @author Gary Gregory * @author Phil Steitz * @author Al Chou * @author Michael Davey * @author Reuben Sivan * @author Chris Hyzer * Johnson */ //package com.java2s; public class Main { public static void main(String[] argv) { String str = "java2s.com"; String suffix = "java2s.com"; System.out.println(endsWithIgnoreCase(str, suffix)); } /** * <p>Case insensitive check if a String ends with a specified suffix.</p> * * <p><code>null</code>s are handled without exceptions. Two <code>null</code> * references are considered to be equal. The comparison is case insensitive.</p> * * <pre> * endsWithIgnoreCase(null, null) = true * endsWithIgnoreCase(null, "def") = false * endsWithIgnoreCase("abcdef", null) = false * endsWithIgnoreCase("abcdef", "def") = true * endsWithIgnoreCase("ABCDEF", "def") = true * endsWithIgnoreCase("ABCDEF", "cde") = false * </pre> * * @see java.lang.String#endsWith(String) * @param str the String to check, may be null * @param suffix the suffix to find, may be null * @return <code>true</code> if the String ends with the suffix, case insensitive, or * both <code>null</code> * @since 2.4 */ public static boolean endsWithIgnoreCase(String str, String suffix) { return endsWith(str, suffix, true); } /** * <p>Check if a String ends with a specified suffix.</p> * * <p><code>null</code>s are handled without exceptions. Two <code>null</code> * references are considered to be equal. The comparison is case sensitive.</p> * * <pre> * endsWith(null, null) = true * endsWith(null, "def") = false * endsWith("abcdef", null) = false * endsWith("abcdef", "def") = true * endsWith("ABCDEF", "def") = false * endsWith("ABCDEF", "cde") = false * </pre> * * @see java.lang.String#endsWith(String) * @param str the String to check, may be null * @param suffix the suffix to find, may be null * @return <code>true</code> if the String ends with the suffix, case sensitive, or * both <code>null</code> * @since 2.4 */ public static boolean endsWith(String str, String suffix) { return endsWith(str, suffix, false); } /** * <p>Check if a String ends with a specified suffix (optionally case insensitive).</p> * * @see java.lang.String#endsWith(String) * @param str the String to check, may be null * @param suffix the suffix to find, may be null * @param ignoreCase inidicates whether the compare should ignore case * (case insensitive) or not. * @return <code>true</code> if the String starts with the prefix or * both <code>null</code> */ private static boolean endsWith(String str, String suffix, boolean ignoreCase) { if (str == null || suffix == null) { return (str == null && suffix == null); } if (suffix.length() > str.length()) { return false; } int strOffset = str.length() - suffix.length(); return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length()); } /** * Gets a String's length or <code>0</code> if the String is <code>null</code>. * * @param str * a String or <code>null</code> * @return String length or <code>0</code> if the String is <code>null</code>. * @since 2.4 */ public static int length(String str) { return str == null ? 0 : str.length(); } }