Class StringHelper

java.lang.Object
com.shaiksphere.mindsmine.jems.StringHelper

public final class StringHelper extends Object
A collection of useful static methods to deal with strings.
Since:
1.0
Author:
Mohammed Shaik Hussain Ali
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    areEqual(String string1, String string2)
    Returns true if the passed values are equal, ignoring the case, false otherwise.
    static boolean
    areEqual(String string1, String string2, boolean lenient)
    Returns true if the passed values are equal, false otherwise.
    static String
    Returns a non-null string, even if the object being passed is a null string.
    static boolean
    Returns true if the passed string contains only digits, false otherwise.
    static boolean
    Returns true if the passed string is a palindrome, ignoring the case, false otherwise.
    static boolean
    isPalindrome(String string, boolean lenient)
    Returns true if the passed string is a palindrome, false otherwise.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getNullSafe

      public static String getNullSafe(String string)
      Returns a non-null string, even if the object being passed is a null string.

      If the passed-in object is a non-null string, then it is returned as-is.

      Example usage:
            String str1 = "Hello";
            String str2 = null;
      
            String str3 = StringHelper.getNullSafe(str1);
      
            String str4 = StringHelper.getNullSafe(str2);
      
            // str3 now contains the string: "Hello"
            // str4 now contains the string: ""
       
      Parameters:
      string - The string to safeguard against null.
      Returns:
      If str is null then "" (empty string).
      Since:
      1.0
    • areEqual

      public static boolean areEqual(String string1, String string2)
      Returns true if the passed values are equal, ignoring the case, false otherwise.

      Convenience method equivalent to StringHelper.areEqual(string1, string2, true)
      Parameters:
      string1 - to compare
      string2 - to compare
      Returns:
      whether two strings are equal
      Since:
      1.0
      See Also:
    • areEqual

      public static boolean areEqual(String string1, String string2, boolean lenient)
      Returns true if the passed values are equal, false otherwise.

      • When the lenient flag is set to true, the comparison will ignore the case and trim the strings before comparing; the two strings are considered equal if,
      • When the lenient flag is set to false, the two strings are considered equal if,
        • Both strings are not null
        • Both strings represent the same sequence of characters

      Example usage:
           StringHelper.areEqual(null, null, true)       = true
           StringHelper.areEqual(null, "", true)         = true
           StringHelper.areEqual("", null, true)         = true
           StringHelper.areEqual("", "", true)           = true
           StringHelper.areEqual("   ", "", true)        = true
           StringHelper.areEqual(" abc", "abc ", true)   = true
           StringHelper.areEqual("", "abc", true)        = false
           StringHelper.areEqual("ab c", "abc", true)    = false
           StringHelper.areEqual("ABC", "abc", true)     = true
           StringHelper.areEqual("abc", "abc", true)     = true
      
           StringHelper.areEqual(null, null, false)      = false
           StringHelper.areEqual(null, "", false)        = false
           StringHelper.areEqual("", null, false)        = false
           StringHelper.areEqual("", "", false)          = true
           StringHelper.areEqual("   ", "", false)       = false
           StringHelper.areEqual(" abc", "abc ", false)  = false
           StringHelper.areEqual("", "abc", false)       = false
           StringHelper.areEqual("ab c", "abc", false)   = false
           StringHelper.areEqual("ABC", "abc", false)    = false
           StringHelper.areEqual("abc", "abc", false)    = true
       
      Parameters:
      string1 - to compare
      string2 - to compare
      lenient - whether to be lenient or not
      Returns:
      whether two strings are equal
      Since:
      2.1.0
      See Also:
    • isOnlyDigits

      public static boolean isOnlyDigits(String string)
      Returns true if the passed string contains only digits, false otherwise.

      Example usage:
           StringHelper.isOnlyDigits(null)          = false
           StringHelper.isOnlyDigits("")            = false
           StringHelper.isOnlyDigits(" ")           = false
           StringHelper.isOnlyDigits("something ")  = false
           StringHelper.isOnlyDigits("11.67")       = false
           StringHelper.isOnlyDigits("17650")       = true
       
      Parameters:
      string - to check
      Returns:
      whether the string contains only digits
      Since:
      1.0
      See Also:
    • isPalindrome

      public static boolean isPalindrome(String string)
      Returns true if the passed string is a palindrome, ignoring the case, false otherwise.

      Convenience method equivalent to StringHelper.isPalindrome(string, true)
      Parameters:
      string - to check
      Returns:
      if the string is a Palindrome
      Since:
      2.1.0
      See Also:
    • isPalindrome

      public static boolean isPalindrome(String string, boolean lenient)
      Returns true if the passed string is a palindrome, false otherwise.

      A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward, such as madam or kayak.

      Convenience method equivalent to StringHelper.areEqual(string, string.reverse, flag)

      Example usage:
           StringHelper.isPalindrome(null, true)      = true
           StringHelper.isPalindrome("", true)        = true
           StringHelper.isPalindrome("   ", true)     = true
           StringHelper.isPalindrome(" aba", true)    = true
           StringHelper.isPalindrome("aba", true)     = true
           StringHelper.isPalindrome("mAdAm", true)   = true
           StringHelper.isPalindrome("madAm", true)   = true
           StringHelper.isPalindrome("madam", true)   = true
           StringHelper.isPalindrome("Madam", true)   = true
           StringHelper.isPalindrome("hello", true)   = false
      
           StringHelper.isPalindrome(null, false)     = false
           StringHelper.isPalindrome("", false)       = true
           StringHelper.isPalindrome("   ", false)    = true
           StringHelper.isPalindrome(" aba", false)   = false
           StringHelper.isPalindrome("aba", false)    = true
           StringHelper.isPalindrome("mAdAm", false)  = true
           StringHelper.isPalindrome("madAm", false)  = false
           StringHelper.isPalindrome("madam", false)  = true
           StringHelper.isPalindrome("Madam", false)  = false
           StringHelper.isPalindrome("hello", false)  = false
       
      Parameters:
      string - to check
      lenient - whether to be lenient or not
      Returns:
      if the string is a Palindrome
      Since:
      2.1.0
      See Also: