Package Torello.Java

Class StrCh


  • public class StrCh
    extends java.lang.Object
    Class String-Character provides an exhaustive-combinatoric suite of methods that extend the basic Java String methods equals, contains, startsWith and endsWith, using Java char primitives as the comparator element.

    This class offers an API that has a large collection of comparison routines for a java.lang.String.

    Obviously, Regular-Expressions will always be capable of providing the most powerful String-Search and manipulations routines. However, with the extra level of control that a Regular-Expression offers comes some extra-amount of complexity. The routines in class StrCmpr and class StrIndexOf can eliminate for-loops and all the debugging that comes along with them - saving programmers countless hours of when their programs need only simple comparisons and searches that can be written without a RegEx.

    Example:
    boolean result = StrCh.containsXOR_CI("Visited in August of this year." 'v', 'q');
    // Evaluates to TRUE
    
    boolean result = StrCmpr.containsXOR_CI("Traveled to Beijing last year.", 'b', 'e');
    // Evaluates to FALSE
    

    Methods Available

    Method-Name Key-Word Explanation
    NAND May not find any matches between the primary String srcStr parameter, and the char's passed to 'compareChar'

    NAND-NOTE: There is one very important subtlety about the meaning of 'NAND' here. In this case, the methods that use 'NAND' interpret its meaning as "NOT ANY ONE OF..." - meaning all comparisons must fail.
    XOR Must find precisely one match between the primary String srcStr parameter, and the char's in 'compareChar'.
    OR Must find at least one match between the primary String srcStr parameter, and the char's in the 'compareChar' parameter.
    AND Must identify a containing-match between each and every char in char... compareChar and the primary 'srcStr' parameter.
    Starts With The provided compare-char (or char's) must match the beginning of parameter 'srcStr'.
    Ends With The provided compare-char (or char's) must match the ending of parameter 'srcStr'.
    Contains The provided compare-char (or char's) must be present within parameter 'srcStr'.
    CI (Case Insensitive) All equality-tests will be done on a Case Insensitive basis.


Stateless Class: This class neither contains any program-state, nor can it be instantiated. The @StaticFunctional Annotation may also be called 'The Spaghetti Report'. Static-Functional classes are, essentially, C-Styled Files, without any constructors or non-static member field. It is very similar to the Java-Bean @Stateless Annotation.
  • 1 Constructor(s), 1 declared private, zero-argument constructor
  • 21 Method(s), 21 declared static
  • 4 Field(s), 4 declared static, 4 declared final


    • Field Summary

       
      Internal, Protected Helper Fields
      Modifier and Type Field
      protected static byte AND
      protected static byte NAND
      protected static byte OR
      protected static byte XOR
    • Method Summary

       
      Single Input-Character Comparisons
      Modifier and Type Method
      static boolean containsIgnoreCase​(String srcStr, char compareChar)
      static boolean endsWithIgnoreCase​(String srcStr, char compareChar)
      static boolean startsWithIgnoreCase​(String srcStr, char compareChar)
       
      Contains Comparisons
      Modifier and Type Method
      static boolean containsAND​(String srcStr, char... compareChar)
      static boolean containsAND_CI​(String srcStr, char... compareChar)
      static boolean containsNAND​(String srcStr, char... compareChar)
      static boolean containsNAND_CI​(String srcStr, char... compareChar)
      static boolean containsOR​(String srcStr, char... compareChar)
      static boolean containsOR_CI​(String srcStr, char... compareChar)
      static boolean containsXOR​(String srcStr, char... compareChar)
      static boolean containsXOR_CI​(String srcStr, char... compareChar)
       
      Starts-With Comparisons
      Modifier and Type Method
      static boolean startsWithNAND​(String srcStr, char... compareChar)
      static boolean startsWithNAND_CI​(String srcStr, char... compareChar)
      static boolean startsWithOR​(String srcStr, char... compareChar)
      static boolean startsWithOR_CI​(String srcStr, char... compareChar)
       
      Ends-With Comparisons
      Modifier and Type Method
      static boolean endsWithNAND​(String srcStr, char... compareChar)
      static boolean endsWithNAND_CI​(String srcStr, char... compareChar)
      static boolean endsWithOR​(String srcStr, char... compareChar)
      static boolean endsWithOR_CI​(String srcStr, char... compareChar)
       
      Internal, Protected Methods
      Modifier and Type Method
      protected static boolean CONTAINS_NAND_OR​(boolean ignoreCase, byte booleanOperation, String srcStr, char[] compareChar)
      protected static boolean CONTAINS_XOR_AND​(boolean ignoreCase, byte booleanOperation, String srcStr, char[] compareChar)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • AND

        🡇    
        protected static final byte AND
        Signifies that an 'AND' operation is required, but only for methods that implement one of the 'contains' variants.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
        protected static final byte AND = 0;
        
      • OR

        🡅  🡇    
        protected static final byte OR
        Signifies that an 'AND' operation is required, but only for methods that implement one of the 'contains' variants.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
        protected static final byte OR = 1;
        
      • NAND

        🡅  🡇    
        protected static final byte NAND
        Signifies that an 'AND' operation is required, but only for methods that implement one of the 'contains' variants.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
        protected static final byte NAND = 2;
        
      • XOR

        🡅  🡇    
        protected static final byte XOR
        Signifies that an 'AND' operation is required, but only for methods that implement one of the 'contains' variants.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
        protected static final byte XOR = 3;
        
    • Method Detail

      • containsOR

        🡅  🡇    
        public static boolean containsOR​(java.lang.String srcStr,
                                         char... compareChar)
        Checks to ensure that srcStr contains at least one of the char's inside the 'compareChar' array.

        The test performed here ** is ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr contains at least one of the 'compareChar' characters (and FALSE otherwise).
        See Also:
        CONTAINS_NAND_OR(boolean, byte, String, char[])
        Code:
        Exact Method Body:
         return CONTAINS_NAND_OR(false, OR, srcStr, compareChar);
        
      • containsAND

        🡅  🡇    
        public static boolean containsAND​(java.lang.String srcStr,
                                          char... compareChar)
        Checks to ensure that srcStr contains every one of the char's inside the 'compareChar' array.

        The test performed here ** is ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr contains every one of the 'compareChar' characters (and FALSE otherwise).
        See Also:
        CONTAINS_XOR_AND(boolean, byte, String, char[])
        Code:
        Exact Method Body:
         return CONTAINS_XOR_AND(false, AND, srcStr, compareChar);
        
      • containsXOR

        🡅  🡇    
        public static boolean containsXOR​(java.lang.String srcStr,
                                          char... compareChar)
        Checks to ensure that srcStr contains exactly one of the char's inside the 'compareChar' array.

        The test performed here ** is ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr contains exactly one of the 'compareChar' characters (and FALSE otherwise).

        An internal counter is maintained, and it must equal precisely one prior to exit in order for TRUE to be returned. In the case that multiple copies of an identical char have been passed into parameter char[] compareChar, the returned-answer's is best considered non-deterministic..

        NOTE: No exception checks are performed on compareChar for duplicate elements in that array. This check is avoied essentially due to the performance over-head of checking for it.
        See Also:
        CONTAINS_XOR_AND(boolean, byte, String, char[])
        Code:
        Exact Method Body:
         return CONTAINS_XOR_AND(false, XOR, srcStr, compareChar);
        
      • containsNAND

        🡅  🡇    
        public static boolean containsNAND​(java.lang.String srcStr,
                                           char... compareChar)
        Checks to ensure that srcStr does not contain any of the char's inside the 'compareChar' array.

        The test performed here ** is ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr does not contain any of the 'compareChar' characters (and FALSE otherwise).
        See Also:
        CONTAINS_NAND_OR(boolean, byte, String, char[])
        Code:
        Exact Method Body:
         return CONTAINS_NAND_OR(false, NAND, srcStr, compareChar);
        
      • containsOR_CI

        🡅  🡇    
        public static boolean containsOR_CI​(java.lang.String srcStr,
                                            char... compareChar)
        Checks to ensure that srcStr contains at least one of the char's inside the 'compareChar' array.

        The test performed here ** is not ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr contains at least one of the 'compareChar' characters (and FALSE otherwise).
        See Also:
        CONTAINS_NAND_OR(boolean, byte, String, char[])
        Code:
        Exact Method Body:
         return CONTAINS_NAND_OR(true, OR, srcStr, compareChar);
        
      • containsAND_CI

        🡅  🡇    
        public static boolean containsAND_CI​(java.lang.String srcStr,
                                             char... compareChar)
        Checks to ensure that srcStr contains every one of the char's inside the 'compareChar' array.

        The test performed here ** is not ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr contains every one of the 'compareChar' characters (and FALSE otherwise).
        See Also:
        CONTAINS_XOR_AND(boolean, byte, String, char[])
        Code:
        Exact Method Body:
         return CONTAINS_XOR_AND(true, AND, srcStr, compareChar);
        
      • containsXOR_CI

        🡅  🡇    
        public static boolean containsXOR_CI​(java.lang.String srcStr,
                                             char... compareChar)
        Checks to ensure that srcStr contains exactly one of the char's inside the 'compareChar' array.

        The test performed here ** is not ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr contains exactly one of the 'compareChar' characters (and FALSE otherwise).

        An internal counter is maintained, and it must equal precisely one prior to exit in order for TRUE to be returned. In the case that multiple copies of an identical char have been passed into parameter char[] compareChar, the returned-answer's is best considered non-deterministic..

        NOTE: No exception checks are performed on compareChar for duplicate elements in that array. This check is avoied essentially due to the performance over-head of checking for it.
        See Also:
        CONTAINS_XOR_AND(boolean, byte, String, char[])
        Code:
        Exact Method Body:
         return CONTAINS_XOR_AND(true, XOR, srcStr, compareChar);
        
      • containsNAND_CI

        🡅  🡇    
        public static boolean containsNAND_CI​(java.lang.String srcStr,
                                              char... compareChar)
        Checks to ensure that srcStr does not contain any of the char's inside the 'compareChar' array.

        The test performed here ** is not ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr does not contain any of the 'compareChar' characters (and FALSE otherwise).
        See Also:
        CONTAINS_NAND_OR(boolean, byte, String, char[])
        Code:
        Exact Method Body:
         return CONTAINS_NAND_OR(true, NAND, srcStr, compareChar);
        
      • endsWithOR

        🡅  🡇    
        public static boolean endsWithOR​(java.lang.String srcStr,
                                         char... compareChar)
        Checks to ensure that srcStr ends with at least one of the char's inside the 'compareChar' array.

        The test performed here ** is ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr ends with at least one of the 'compareChar' characters (and FALSE otherwise).
        Code:
        Exact Method Body:
         char c = srcStr.charAt(srcStr.length() - 1);
         for (char c2 : compareChar) if (c2 == c) return true;
         return false;
        
      • endsWithNAND

        🡅  🡇    
        public static boolean endsWithNAND​(java.lang.String srcStr,
                                           char... compareChar)
        Checks to ensure that srcStr does not end with any of the char's inside the 'compareChar' array.

        The test performed here ** is ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr does not end with any of the 'compareChar' characters (and FALSE otherwise).
        Code:
        Exact Method Body:
         char c = srcStr.charAt(srcStr.length() - 1);
         for (char c2 : compareChar) if (c2 == c) return false;
         return true;
        
      • startsWithOR

        🡅  🡇    
        public static boolean startsWithOR​(java.lang.String srcStr,
                                           char... compareChar)
        Checks to ensure that srcStr starts with at least one of the char's inside the 'compareChar' array.

        The test performed here ** is ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr starts with at least one of the 'compareChar' characters (and FALSE otherwise).
        Code:
        Exact Method Body:
         char c = srcStr.charAt(0);
         for (char c2 : compareChar) if (c2 == c) return true;
         return false;
        
      • startsWithNAND

        🡅  🡇    
        public static boolean startsWithNAND​(java.lang.String srcStr,
                                             char... compareChar)
        Checks to ensure that srcStr does not start with any of the char's inside the 'compareChar' array.

        The test performed here ** is ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr does not start with any of the 'compareChar' characters (and FALSE otherwise).
        Code:
        Exact Method Body:
         char c = srcStr.charAt(0);
         for (char c2 : compareChar) if (c2 == c) return false;
         return true;
        
      • endsWithOR_CI

        🡅  🡇    
        public static boolean endsWithOR_CI​(java.lang.String srcStr,
                                            char... compareChar)
        Checks to ensure that srcStr ends with at least one of the char's inside the 'compareChar' array.

        The test performed here ** is not ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr ends with at least one of the 'compareChar' characters (and FALSE otherwise).
        Code:
        Exact Method Body:
         char c = Character.toLowerCase(srcStr.charAt(srcStr.length() - 1));
         for (char c2 : compareChar) if (Character.toLowerCase(c2) == c) return true;
         return false;
        
      • endsWithNAND_CI

        🡅  🡇    
        public static boolean endsWithNAND_CI​(java.lang.String srcStr,
                                              char... compareChar)
        Checks to ensure that srcStr does not end with any of the char's inside the 'compareChar' array.

        The test performed here ** is not ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr does not end with any of the 'compareChar' characters (and FALSE otherwise).
        Code:
        Exact Method Body:
         char c = Character.toLowerCase(srcStr.charAt(srcStr.length() - 1));
         for (char c2 : compareChar) if (Character.toLowerCase(c2) == c) return false;
         return true;
        
      • startsWithOR_CI

        🡅  🡇    
        public static boolean startsWithOR_CI​(java.lang.String srcStr,
                                              char... compareChar)
        Checks to ensure that srcStr starts at least one of the char's inside the 'compareChar' array.

        The test performed here ** is not ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr starts at least one of the 'compareChar' characters (and FALSE otherwise).
        Code:
        Exact Method Body:
         char c = Character.toLowerCase(srcStr.charAt(0));
         for (char c2 : compareChar) if (Character.toLowerCase(c2) == c) return true;
         return false;
        
      • startsWithNAND_CI

        🡅  🡇    
        public static boolean startsWithNAND_CI​(java.lang.String srcStr,
                                                char... compareChar)
        Checks to ensure that srcStr does not start with any of the char's inside the 'compareChar' array.

        The test performed here ** is not ** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char's used in the comparison against 'srcStr'
        Returns:
        This will return TRUE if and only if srcStr does not start with any of the 'compareChar' characters (and FALSE otherwise).
        Code:
        Exact Method Body:
         char c = Character.toLowerCase(srcStr.charAt(0));
         for (char c2 : compareChar) if (Character.toLowerCase(c2) == c) return false;
         return true;
        
      • startsWithIgnoreCase

        🡅  🡇    
        public static boolean startsWithIgnoreCase​(java.lang.String srcStr,
                                                   char compareChar)
        Checks whether or not 'srcStr' starts-with 'compareChar'.

        This method ** is not** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char used in the comparison against 'srcStr'
        Returns:
        TRUE if 'srcStr' begins with 'compareChar', ignoring case.
        Code:
        Exact Method Body:
         return Character.toLowerCase(srcStr.charAt(0)) == Character.toLowerCase(compareChar);
        
      • endsWithIgnoreCase

        🡅  🡇    
        public static boolean endsWithIgnoreCase​(java.lang.String srcStr,
                                                 char compareChar)
        Checks whether or not 'srcStr' ends-with 'compareChar'.

        This method ** is not** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char used in the comparison against 'srcStr'
        Returns:
        TRUE if 'srcStr' ends with 'compareChar', ignoring case.
        Code:
        Exact Method Body:
         return Character.toLowerCase(srcStr.charAt(srcStr.length() - 1)) == 
             Character.toLowerCase(compareChar);
        
      • containsIgnoreCase

        🡅  🡇    
        public static boolean containsIgnoreCase​(java.lang.String srcStr,
                                                 char compareChar)
        Checks whether or not 'srcStr' contains 'compareChar'.

        This method ** is not** case-sensitive.
        Parameters:
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char used in the comparison against 'srcStr'
        Returns:
        TRUE if 'srcStr' begins with 'compareChar', ignoring case.
        Code:
        Exact Method Body:
         compareChar = Character.toLowerCase(compareChar);
        
         int len = srcStr.length();
        
         for (int i=0; i < len; i++)
             if (Character.toLowerCase(srcStr.charAt(i)) == compareChar)
                 return true;
        
         return false;
        
      • CONTAINS_NAND_OR

        🡅  🡇    
        protected static boolean CONTAINS_NAND_OR​(boolean ignoreCase,
                                                  byte booleanOperation,
                                                  java.lang.String srcStr,
                                                  char[] compareChar)
        A protected helper method for NAND and OR 'contains' methods.

        NOTE: Does not error check the value passed to 'booleanOperation'.
        Parameters:
        ignoreCase - Whether the comparisons performed should ignore case.
        booleanOperation - Accepts only NAND and OR.
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char used in the comparison against 'srcStr'
        Code:
        Exact Method Body:
         if (ignoreCase)
         {
             char[] temp = new char[compareChar.length];
        
             for (int i=0; i < compareChar.length; i++)
                 temp[i] = Character.toLowerCase(compareChar[i]);
        
             compareChar = temp;
         }
        
         int len = srcStr.length();
        
         for (int i=0; i < len; i++)
         {
             char c = ignoreCase ? Character.toLowerCase(srcStr.charAt(i)) : srcStr.charAt(i);
        
             for (char c2 : compareChar)
        
                 if (c2 == c)
        
                     switch(booleanOperation)
                     {
                         case NAND:  return false;
                         case OR:    return true;
                     }
         }
        
         switch (booleanOperation)
         {
             case NAND:  return true;
             case OR:    return false;
         }
        
         throw new UnreachableError();
        
      • CONTAINS_XOR_AND

        🡅    
        protected static boolean CONTAINS_XOR_AND​(boolean ignoreCase,
                                                  byte booleanOperation,
                                                  java.lang.String srcStr,
                                                  char[] compareChar)
        A protected helper method for XOR and AND 'contains' methods.

        NOTE: Does not error check the value passed to 'booleanOperation'.
        Parameters:
        ignoreCase - Whether the comparisons performed should ignore case.
        booleanOperation - Accepts only XOR and AND.
        srcStr - Any non-null instance of java.lang.String
        compareChar - The char used in the comparison against 'srcStr'
        Code:
        Exact Method Body:
         int count = 0;
        
         // There is a TreeSet so that multiple instances of the same character ARE NOT CHECKED.
         // Once a character has been found, it is more efficient to stop testing for it at all.
        
         TreeSet<Character> ts = new TreeSet<>();
        
         // When building the TreeSet of characters to check, if 'ignoreCase' has been requested,
         // then doing the case-conversion before entering the loop is more efficient.
        
         if (ignoreCase) for (char c : compareChar) ts.add(Character.toLowerCase(c));
         else            for (char c : compareChar) ts.add(c);
        
         // Iterate each character in the passed 'srcStr'
         int len = srcStr.length();
        
         for (int i=0; i < len; i++)
         {
             char c = ignoreCase ? Character.toLowerCase(srcStr.charAt(i)) : srcStr.charAt(i);
        
             // Iterate each of the remaining compare-characters in the TreeSet.  These characters
             // are removed when matches are found.
        
             INNER:
             for (char c2 : ts)
        
                 if (c2 == c)
                 {
                     ts.remove(c2);
        
                     switch (booleanOperation)
                     {
                         case AND: if (--count == 0) return true; else break INNER;
                         case XOR: if (++count > 1)  return false; else break INNER;
                     }
                 }
         }
        
         switch (booleanOperation)
         {
             // If the count had reached zero, then true would ALREADY have been returned.
             case AND: return false;
        
             // Whether or not a match has been found is all that is left to check.
             case XOR: return count == 1;
         }
        
         throw new UnreachableError();