Package Torello.Java
Class StrCh
- java.lang.Object
-
- Torello.Java.StrCh
-
public class StrCh extends java.lang.Object
Class String-Character provides an exhaustive-combinatoric suite of methods that extend the basic JavaStringmethodsequals, contains, startsWithandendsWith, using Javacharprimitives as the comparator element.
This class offers an API that has a large collection of comparison routines for ajava.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 inclass StrCmprandclass StrIndexOfcan eliminatefor-loopsand 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 srcStrparameter, and thechar'spassed 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 srcStrparameter, and thechar'sin'compareChar'.OR Must find at least one match between the primary String srcStrparameter, and thechar'sin the'compareChar'parameter.AND Must identify a containing-match between each and every charinchar... compareCharand the primary'srcStr'parameter.Starts With The provided compare-char(orchar's) must match the beginning of parameter'srcStr'.Ends With The provided compare-char(orchar's) must match the ending of parameter'srcStr'.Contains The provided compare-char(orchar's) must be present within parameter'srcStr'.CI (Case Insensitive) All equality-tests will be done on a Case Insensitivebasis.
Hi-Lited Source-Code:- View Here: Torello/Java/StrCh.java
- Open New Browser-Tab: Torello/Java/StrCh.java
File Size: 19,677 Bytes Line Count: 449 '\n' Characters Found
Stateless Class:This class neither contains any program-state, nor can it be instantiated. The@StaticFunctionalAnnotation may also be called 'The Spaghetti Report'.Static-Functionalclasses are, essentially, C-Styled Files, without any constructors or non-static member fields. It is a concept very similar to the Java-Bean's@StatelessAnnotation.
- 1 Constructor(s), 1 declared private, zero-argument constructor
- 21 Method(s), 21 declared static
- 4 Field(s), 4 declared static, 4 declared final
-
-
Method Summary
Single Input-Character Comparisons Modifier and Type Method static booleancontainsIgnoreCase(String srcStr, char compareChar)static booleanendsWithIgnoreCase(String srcStr, char compareChar)static booleanstartsWithIgnoreCase(String srcStr, char compareChar)Contains Comparisons Modifier and Type Method static booleancontainsAND(String srcStr, char... compareChar)static booleancontainsAND_CI(String srcStr, char... compareChar)static booleancontainsNAND(String srcStr, char... compareChar)static booleancontainsNAND_CI(String srcStr, char... compareChar)static booleancontainsOR(String srcStr, char... compareChar)static booleancontainsOR_CI(String srcStr, char... compareChar)static booleancontainsXOR(String srcStr, char... compareChar)static booleancontainsXOR_CI(String srcStr, char... compareChar)Starts-With Comparisons Modifier and Type Method static booleanstartsWithNAND(String srcStr, char... compareChar)static booleanstartsWithNAND_CI(String srcStr, char... compareChar)static booleanstartsWithOR(String srcStr, char... compareChar)static booleanstartsWithOR_CI(String srcStr, char... compareChar)Ends-With Comparisons Modifier and Type Method static booleanendsWithNAND(String srcStr, char... compareChar)static booleanendsWithNAND_CI(String srcStr, char... compareChar)static booleanendsWithOR(String srcStr, char... compareChar)static booleanendsWithOR_CI(String srcStr, char... compareChar)Internal, Protected Methods Modifier and Type Method protected static booleanCONTAINS_NAND_OR(boolean ignoreCase, byte booleanOperation, String srcStr, char[] compareChar)protected static booleanCONTAINS_XOR_AND(boolean ignoreCase, byte booleanOperation, String srcStr, char[] compareChar)
-
-
-
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 thatsrcStrcontains at least one of thechar'sinside the'compareChar'array.The test performed here ** is ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrcontains at least one of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrcontains every one of thechar'sinside the'compareChar'array.The test performed here ** is ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrcontains every one of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrcontains exactly one of thechar'sinside the'compareChar'array.The test performed here ** is ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrcontains exactly one of the'compareChar'characters (andFALSEotherwise).
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 identicalcharhave been passed into parameterchar[] compareChar, the returned-answer's is best considered non-deterministic..Note: No exception checks are performed oncompareCharfor 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 thatsrcStrdoes not contain any of thechar'sinside the'compareChar'array.The test performed here ** is ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrdoes not contain any of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrcontains at least one of thechar'sinside the'compareChar'array.The test performed here ** is not ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrcontains at least one of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrcontains every one of thechar'sinside the'compareChar'array.The test performed here ** is not ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrcontains every one of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrcontains exactly one of thechar'sinside the'compareChar'array.The test performed here ** is not ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrcontains exactly one of the'compareChar'characters (andFALSEotherwise).
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 identicalcharhave been passed into parameterchar[] compareChar, the returned-answer's is best considered non-deterministic..Note: No exception checks are performed oncompareCharfor 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 thatsrcStrdoes not contain any of thechar'sinside the'compareChar'array.The test performed here ** is not ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrdoes not contain any of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrends with at least one of thechar'sinside the'compareChar'array.The test performed here ** is ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrends with at least one of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrdoes not end with any of thechar'sinside the'compareChar'array.The test performed here ** is ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrdoes not end with any of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrstarts with at least one of thechar'sinside the'compareChar'array.The test performed here ** is ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrstarts with at least one of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrdoes not start with any of thechar'sinside the'compareChar'array.The test performed here ** is ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrdoes not start with any of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrends with at least one of thechar'sinside the'compareChar'array.The test performed here ** is not ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrends with at least one of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrdoes not end with any of thechar'sinside the'compareChar'array.The test performed here ** is not ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrdoes not end with any of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrstarts at least one of thechar'sinside the'compareChar'array.The test performed here ** is not ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrstarts at least one of the'compareChar'characters (andFALSEotherwise). - 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 thatsrcStrdoes not start with any of thechar'sinside the'compareChar'array.The test performed here ** is not ** case-sensitive.- Parameters:
srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thechar'sused in the comparison against'srcStr'- Returns:
- This will return
TRUEif and only ifsrcStrdoes not start with any of the'compareChar'characters (andFALSEotherwise). - 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 ofjava.lang.StringcompareChar- Thecharused in the comparison against'srcStr'- Returns:
TRUEif'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 ofjava.lang.StringcompareChar- Thecharused in the comparison against'srcStr'- Returns:
TRUEif'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 ofjava.lang.StringcompareChar- Thecharused in the comparison against'srcStr'- Returns:
TRUEif'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 forNANDandOR'contains' methods.
NOTE: Does not error check the value passed to'booleanOperation'.- Parameters:
ignoreCase- Whether the comparisons performed should ignore case.booleanOperation- Accepts onlyNANDandOR.srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thecharused 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 forXORandAND'contains' methods.
NOTE: Does not error check the value passed to'booleanOperation'.- Parameters:
ignoreCase- Whether the comparisons performed should ignore case.booleanOperation- Accepts onlyXORandAND.srcStr- Any non-null instance ofjava.lang.StringcompareChar- Thecharused 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();
-
-