Example usage for org.apache.commons.lang3 CharSequenceUtils regionMatches

List of usage examples for org.apache.commons.lang3 CharSequenceUtils regionMatches

Introduction

In this page you can find the example usage for org.apache.commons.lang3 CharSequenceUtils regionMatches.

Prototype

static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
        final CharSequence substring, final int start, final int length) 

Source Link

Document

Green implementation of regionMatches.

Usage

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Compares two CharSequences, returning {@code true} if they are equal ignoring
 * the case.</p>//www  .  j a  v a 2  s. c  o m
 *
 * <p>{@code null}s are handled without exceptions. Two {@code null}
 * references are considered equal. Comparison is case insensitive.</p>
 *
 * <pre>
 * StringUtils.equalsIgnoreCase(null, null)   = true
 * StringUtils.equalsIgnoreCase(null, "abc")  = false
 * StringUtils.equalsIgnoreCase("abc", null)  = false
 * StringUtils.equalsIgnoreCase("abc", "abc") = true
 * StringUtils.equalsIgnoreCase("abc", "ABC") = true
 * </pre>
 *
 * @param str1  the first CharSequence, may be null
 * @param str2  the second CharSequence, may be null
 * @return {@code true} if the CharSequence are equal, case insensitive, or
 *  both {@code null}
 * @since 3.0 Changed signature from equalsIgnoreCase(String, String) to equalsIgnoreCase(CharSequence, CharSequence)
 */
public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2) {
    if (str1 == null || str2 == null) {
        return str1 == str2;
    } else {
        return CharSequenceUtils.regionMatches(str1, true, 0, str2, 0, Math.max(str1.length(), str2.length()));
    }
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Case in-sensitive find of the first index within a CharSequence
 * from the specified position.</p>
 *
 * <p>A {@code null} CharSequence will return {@code -1}.
 * A negative start position is treated as zero.
 * An empty ("") search CharSequence always matches.
 * A start position greater than the string length only matches
 * an empty search CharSequence.</p>
 *
 * <pre>// www.j a  v  a 2 s.  c om
 * StringUtils.indexOfIgnoreCase(null, *, *)          = -1
 * StringUtils.indexOfIgnoreCase(*, null, *)          = -1
 * StringUtils.indexOfIgnoreCase("", "", 0)           = 0
 * StringUtils.indexOfIgnoreCase("aabaabaa", "A", 0)  = 0
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 0)  = 2
 * StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 0) = 1
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 3)  = 5
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", 9)  = -1
 * StringUtils.indexOfIgnoreCase("aabaabaa", "B", -1) = 2
 * StringUtils.indexOfIgnoreCase("aabaabaa", "", 2)   = 2
 * StringUtils.indexOfIgnoreCase("abc", "", 9)        = 3
 * </pre>
 *
 * @param str  the CharSequence to check, may be null
 * @param searchStr  the CharSequence to find, may be null
 * @param startPos  the start position, negative treated as zero
 * @return the first index of the search CharSequence,
 *  -1 if no match or {@code null} string input
 * @since 2.5
 * @since 3.0 Changed signature from indexOfIgnoreCase(String, String, int) to indexOfIgnoreCase(CharSequence, CharSequence, int)
 */
public static int indexOfIgnoreCase(CharSequence str, CharSequence searchStr, int startPos) {
    if (str == null || searchStr == null) {
        return INDEX_NOT_FOUND;
    }
    if (startPos < 0) {
        startPos = 0;
    }
    int endLimit = str.length() - searchStr.length() + 1;
    if (startPos > endLimit) {
        return INDEX_NOT_FOUND;
    }
    if (searchStr.length() == 0) {
        return startPos;
    }
    for (int i = startPos; i < endLimit; i++) {
        if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, searchStr.length())) {
            return i;
        }
    }
    return INDEX_NOT_FOUND;
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Case in-sensitive find of the last index within a CharSequence
 * from the specified position.</p>
 *
 * <p>A {@code null} CharSequence will return {@code -1}.
 * A negative start position returns {@code -1}.
 * An empty ("") search CharSequence always matches unless the start position is negative.
 * A start position greater than the string length searches the whole string.</p>
 *
 * <pre>/*from   w w w .  j  av  a  2  s . co m*/
 * StringUtils.lastIndexOfIgnoreCase(null, *, *)          = -1
 * StringUtils.lastIndexOfIgnoreCase(*, null, *)          = -1
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 8)  = 7
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 8)  = 5
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB", 8) = 4
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 9)  = 5
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", -1) = -1
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 0)  = 0
 * StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 0)  = -1
 * </pre>
 *
 * @param str  the CharSequence to check, may be null
 * @param searchStr  the CharSequence to find, may be null
 * @param startPos  the start position
 * @return the first index of the search CharSequence,
 *  -1 if no match or {@code null} input
 * @since 2.5
 * @since 3.0 Changed signature from lastIndexOfIgnoreCase(String, String, int) to lastIndexOfIgnoreCase(CharSequence, CharSequence, int)
 */
public static int lastIndexOfIgnoreCase(CharSequence str, CharSequence searchStr, int startPos) {
    if (str == null || searchStr == null) {
        return INDEX_NOT_FOUND;
    }
    if (startPos > str.length() - searchStr.length()) {
        startPos = str.length() - searchStr.length();
    }
    if (startPos < 0) {
        return INDEX_NOT_FOUND;
    }
    if (searchStr.length() == 0) {
        return startPos;
    }

    for (int i = startPos; i >= 0; i--) {
        if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, searchStr.length())) {
            return i;
        }
    }
    return INDEX_NOT_FOUND;
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Checks if CharSequence contains a search CharSequence irrespective of case,
 * handling {@code null}. Case-insensitivity is defined as by
 * {@link String#equalsIgnoreCase(String)}.
 *
 * <p>A {@code null} CharSequence will return {@code false}.</p>
 *
 * <pre>/*from  w ww  . j  a v a 2  s  .  co  m*/
 * StringUtils.contains(null, *) = false
 * StringUtils.contains(*, null) = false
 * StringUtils.contains("", "") = true
 * StringUtils.contains("abc", "") = true
 * StringUtils.contains("abc", "a") = true
 * StringUtils.contains("abc", "z") = false
 * StringUtils.contains("abc", "A") = true
 * StringUtils.contains("abc", "Z") = false
 * </pre>
 *
 * @param str  the CharSequence to check, may be null
 * @param searchStr  the CharSequence to find, may be null
 * @return true if the CharSequence contains the search CharSequence irrespective of
 * case or false if not or {@code null} string input
 * @since 3.0 Changed signature from containsIgnoreCase(String, String) to containsIgnoreCase(CharSequence, CharSequence)
 */
public static boolean containsIgnoreCase(CharSequence str, CharSequence searchStr) {
    if (str == null || searchStr == null) {
        return false;
    }
    int len = searchStr.length();
    int max = str.length() - len;
    for (int i = 0; i <= max; i++) {
        if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, len)) {
            return true;
        }
    }
    return false;
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Check if a CharSequence starts with a specified prefix (optionally case insensitive).</p>
 *
 * @see java.lang.String#startsWith(String)
 * @param str  the CharSequence to check, may be null
 * @param prefix the prefix to find, may be null
 * @param ignoreCase indicates whether the compare should ignore case
 *  (case insensitive) or not./*from   w  w w.  j a va 2  s  .co  m*/
 * @return {@code true} if the CharSequence starts with the prefix or
 *  both {@code null}
 */
private static boolean startsWith(CharSequence str, CharSequence prefix, boolean ignoreCase) {
    if (str == null || prefix == null) {
        return str == null && prefix == null;
    }
    if (prefix.length() > str.length()) {
        return false;
    }
    return CharSequenceUtils.regionMatches(str, ignoreCase, 0, prefix, 0, prefix.length());
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Check if a CharSequence ends with a specified suffix (optionally case insensitive).</p>
 *
 * @see java.lang.String#endsWith(String)
 * @param str  the CharSequence to check, may be null
 * @param suffix the suffix to find, may be null
 * @param ignoreCase indicates whether the compare should ignore case
 *  (case insensitive) or not.//from  w  ww  .  ja v  a2 s .  c  o m
 * @return {@code true} if the CharSequence starts with the prefix or
 *  both {@code null}
 */
private static boolean endsWith(CharSequence str, CharSequence 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 CharSequenceUtils.regionMatches(str, ignoreCase, strOffset, suffix, 0, suffix.length());
}