Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-704

New methods for StringUtils class: equalsAny and equalsAnyIgnoreCase

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor
    • Resolution: Duplicate
    • None
    • Review Patch
    • lang.*
    • N/A - it is a feature request.

    Description

      It would be very good if the StringUtils class provided 2 methods for testing String equality with more than one value at a time.
      Methods could be written as follows:

      StringUtils.java
          /**
           * Verifies if the tested string is equal with any of the provided strings.
           * This method is null safe and case sensitive.
           * 
           * @param str Tested string
           * @param any Strings to be tested against.
           * @return true if tested string is equal to any of the provided strings. false otherwise.
           */
          public static boolean equalsWithAny(String str, String... any) {
              if (str == null) {
                  return false;
              }
              
              if (any == null) {
                  return false;
              }
              
              for (String s : any) {
                  if (str.equals(s)) {
                      return true;
                  }
              }
              
              return false;
          }
          
          /**
           * Verifies if the tested string is equal with any of the provided strings.
           * This method is null safe and case insensitive.
           * 
           * @param str Tested string
           * @param any Strings to be tested against.
           * @return true if tested string is equal to any of the provided strings. false otherwise.
           */    
          public static boolean equalsWithAnyIgnoreCase(String str, String... any) {
              if (str == null) {
                  return false;
              }
              
              if (any == null) {
                  return false;
              }
              
              for (String s : any) {
                  if (str.equalsIgnoreCase(s)) {
                      return true;
                  }
              }
              
              return false;
          }
      

      Attachments

        1. StringUtils.java.patch
          3 kB
          Robert Parr
        2. StringUtilsTest.java.patch
          2 kB
          Robert Parr

        Issue Links

          Activity

            People

              Unassigned Unassigned
              rglowinski Rafal Glowinski
              Votes:
              2 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: