Class Strings


  • public final class Strings
    extends Object
    A set of util methods to work with strings. Note: For OpenL rules only! Don't use it in Java code.
    Author:
    Yury Molchan
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String concatenate​(Object... objects)  
      static boolean contains​(String str, char searchChar)
      Checks if String contains a search character, handling null.
      static boolean contains​(String str, String searchStr)
      Checks if String contains a search String, handling null.
      static boolean containsAny​(String str, char... chars)
      Checks if the String contains any character in the given set of characters.
      static boolean containsAny​(String str, String searchChars)
      Checks if the String contains any character in the given set of characters.
      static boolean endsWith​(String str, String suffix)
      Check if a String ends with a specified suffix.

      Two null references are considered to be equal.
      static String format​(String pattern, Object... args)
      Format string with given pattern and the given arguments.

      Examples:
      static boolean isEmpty​(String str)
      Checks if a String is empty ("") or null or blank (" ").

      isEmpty(null) = true
      isEmpty("") = true
      isEmpty(" ") = true
      isEmpty("bob") = false
      isEmpty(" bob ") = false
      static boolean isNotEmpty​(String str)
      Checks if a String is NOT empty ("") or null or blank (" ").

      isEmpty(null) = false
      isEmpty("") = false
      isEmpty(" ") = false
      isEmpty("bob") = true
      isEmpty(" bob ") = true
      static int length​(String str)
      Returns the length of this string.
      static boolean like​(String str, String pattern)
      Checks if the String is matched using given Pattern.

      Syntax:

      static String lowerCase​(String str)
      Converts a String to lower case

      A null input String returns null.
      static String removeEnd​(String str, String remove)
      Removes a substring only if it is at the end of a source string, otherwise returns the source string.
      static String removeStart​(String str, String remove)
      Removes a substring only if it is at the begining of a source string, otherwise returns the source string.
      static String replace​(String str, String searchString, String replacement)
      Replaces all occurrences of a String within another String

      static String replace​(String str, String searchString, String replacement, int max)
      Replaces a String with another String inside a larger String, for the first max values of the search String.

      A null reference passed to this method is a no-op.
      static boolean startsWith​(String str, String prefix)
      Check if a String starts with a specified prefix.

      Two null references are considered to be equal.
      static String substr​(String str, int pos, int length)  
      static String substring​(String str, int beginIndex)
      Gets a substring from the specified String

      A negative start position can be used to start n characters from the end of the String.

      A null String will return null.
      static String substring​(String str, int beginIndex, int endIndex)
      Gets a substring from the specified String

      A negative start position can be used to start/end n characters from the end of the String.
      static String textJoin​(String delimiter, Object... values)
      Joins an array of objects by the given delimiter to the single string.
      static String[] textSplit​(String delimiter, String str)
      Splits string by given delimiter.

      Examples:

      textSplit(null, null) = null
      textSplit(",", null) = null
      textSplit(null, "a,b,c") = ["a,b,c"]
      textSplit(",", "a,b,c") = ["a", "b", "c"]
      textSplit(",", ",,a,,b,,c,,") = ["a", "b", "c"]
      static Double toDouble​(String str)  
      static Integer toInteger​(String str)  
      static Locale toLocale​(String localeStr)
      Convert string locale to Locale
      static Number toNumber​(String str)  
      static String toString​(Object obj)  
      static String trim​(String str)
      Returns a string whose value is this string, with any leading and trailing whitespace removed.
      static String upperCase​(String str)
      Converts a String to upper case

      A null input String returns null.

    • Method Detail

      • contains

        public static boolean contains​(String str,
                                       String searchStr)

        Checks if String contains a search String, handling null. This method uses String.indexOf(String).

        A null String will return false.

         contains(null, *)     = false
         contains(*, null)     = false
         contains("", "")      = true
         contains("abc", "")   = true
         contains("abc", "a")  = true
         contains("abc", "z")  = false
         
        Parameters:
        str - the String to check, may be null
        searchStr - the String to find, may be null
        Returns:
        true if the String contains the search String, false if not or null string input
      • contains

        public static boolean contains​(String str,
                                       char searchChar)

        Checks if String contains a search character, handling null. This method uses String.indexOf(int).

        A null or empty ("") String will return false.

         contains(null, *)    = false
         contains("", *)      = false
         contains("abc", 'a') = true
         contains("abc", 'z') = false
         
        Parameters:
        str - the String to check, may be null
        searchChar - the character to find
        Returns:
        true if the String contains the search character, false if not or null string input
      • containsAny

        public static boolean containsAny​(String str,
                                          char... chars)

        Checks if the String contains any character in the given set of characters.

        A null String will return false. A null or zero length search array will return false.

         containsAny(null, *)                = false
         containsAny("", *)                  = false
         containsAny(*, null)                = false
         containsAny(*, [])                  = false
         containsAny("zzabyycdxx",['z','a']) = true
         containsAny("zzabyycdxx",['b','y']) = true
         containsAny("aba", ['z'])           = false
         
        Parameters:
        str - the String to check, may be null
        chars - the chars to search for, may be null
        Returns:
        the true if any of the chars are found, false if no match or null input
      • containsAny

        public static boolean containsAny​(String str,
                                          String searchChars)

        Checks if the String contains any character in the given set of characters.

        A null String will return false. A null search string will return false.

         containsAny(null, *)            = false
         containsAny("", *)              = false
         containsAny(*, null)            = false
         containsAny(*, "")              = false
         containsAny("zzabyycdxx", "za") = true
         containsAny("zzabyycdxx", "by") = true
         containsAny("aba","z")          = false
         
        Parameters:
        str - the String to check, may be null
        searchChars - the chars to search for, may be null
        Returns:
        the true if any of the chars are found, false if no match or null input
      • isEmpty

        public static boolean isEmpty​(String str)
        Checks if a String is empty ("") or null or blank (" ").

        isEmpty(null) = true
        isEmpty("") = true
        isEmpty(" ") = true
        isEmpty("bob") = false
        isEmpty(" bob ") = false
        Parameters:
        str - the String to check, may be null
        Returns:
        true if the String is empty or null or blank
      • isNotEmpty

        public static boolean isNotEmpty​(String str)
        Checks if a String is NOT empty ("") or null or blank (" ").

        isEmpty(null) = false
        isEmpty("") = false
        isEmpty(" ") = false
        isEmpty("bob") = true
        isEmpty(" bob ") = true
        Parameters:
        str - the String to check, may be null
        Returns:
        true if the String is NOT empty or null or blank
        See Also:
        isEmpty(String)
      • length

        public static int length​(String str)
        Returns the length of this string.
        Returns:
        the length of the sequence of characters represented by this object, or 0 for null.
      • trim

        public static String trim​(String str)
        Returns a string whose value is this string, with any leading and trailing whitespace removed.
        Returns:
        A string whose value is this string, with any leading and trailing white space removed, or this string if it has no leading or trailing white space.
      • startsWith

        public static boolean startsWith​(String str,
                                         String prefix)
        Check if a String starts with a specified prefix.

        Two null references are considered to be equal. The comparison is case sensitive.

        startsWith(null, null) = true
        startsWith(null, "abc") = false
        startsWith("abcdef", null) = false
        startsWith("abcdef", "abc") = true
        startsWith("ABCDEF", "abc") = false
        Parameters:
        str - the String to check, may be null
        prefix - the prefix to find, may be null
        Returns:
        true if the String starts with the prefix, case sensitive, or both null
      • endsWith

        public static boolean endsWith​(String str,
                                       String suffix)
        Check if a String ends with a specified suffix.

        Two null references are considered to be equal. The comparison is case sensitive.

        endsWith(null, null) = true
        endsWith(null, "def") = false
        endsWith("abcdef", null) = false
        endsWith("abcdef", "def") = true
        endsWith("ABCDEF", "def") = false
        endsWith("ABCDEF", "cde") = false
        Parameters:
        str - the String to check, may be null
        suffix - the suffix to find, may be null
        Returns:
        true if the String ends with the suffix, case sensitive, or both null
      • substr

        public static String substr​(String str,
                                    int pos,
                                    int length)
      • substring

        public static String substring​(String str,
                                       int beginIndex)
        Gets a substring from the specified String

        A negative start position can be used to start n characters from the end of the String.

        A null String will return null. An empty ("") String will return "".

        * substring(null, *) = null
        substring("", *) = ""
        substring("abc", 0) = "abc"
        substring("abc", 2) = "c"
        substring("abc", 4) = ""
        substring("abc", -2) = "bc"
        substring("abc", -4) = "abc"
        Parameters:
        str - the String to get the substring from, may be null
        beginIndex - the position to start from, negative means count back from the end of the String by this many characters
        Returns:
        substring from start position, null if null String input
      • substring

        public static String substring​(String str,
                                       int beginIndex,
                                       int endIndex)
        Gets a substring from the specified String

        A negative start position can be used to start/end n characters from the end of the String.

        The returned substring starts with the character in the start position and ends before the end position. All position counting is zero-based -- i.e., to start at the beginning of the string use start = 0. Negative start and end positions can be used to specify offsets relative to the end of the String.

        If start is not strictly to the left of end, "" is returned.

        substring(null, *, *) = null
        substring("", * , *) = "";
        substring("abc", 0, 2) = "ab"
        substring("abc", 2, 0) = ""
        substring("abc", 2, 4) = "c"
        substring("abc", 4, 6) = ""
        substring("abc", 2, 2) = ""
        substring("abc", -2, -1) = "b"
        substring("abc", -4, 2) = "ab"
        Parameters:
        str - the String to get the substring from, may be null
        beginIndex - the position to start from, negative means count back from the end of the String by this many characters
        endIndex - the position to end at (exclusive), negative means count back from the end of the String by this many characters
        Returns:
        substring from start position to end positon, null if null String input
      • removeStart

        public static String removeStart​(String str,
                                         String remove)
        Removes a substring only if it is at the begining of a source string, otherwise returns the source string.

        A null source string will return null. An empty ("") source string will return the empty string. A null search string will return the source string.

        removeStart(null, *) = null
        removeStart("", *) = ""
        removeStart(*, null) = *
        removeStart("www.domain.com", "www.") = "domain.com"
        removeStart("domain.com", "www.") = "domain.com"
        removeStart("www.domain.com", "domain") = "www.domain.com"
        removeStart("abc", "") = "abc"

        Parameters:
        str - the source String to search, may be null
        remove - the String to search for and remove, may be null
        Returns:
        the substring with the string removed if found, null if null String input
      • removeEnd

        public static String removeEnd​(String str,
                                       String remove)
        Removes a substring only if it is at the end of a source string, otherwise returns the source string.

        A null source string will return null. An empty ("") source string will return the empty string. A null search string will return the source string.

        removeEnd(null, *) = null
        removeEnd("", *) = ""
        removeEnd(*, null) = *
        removeEnd("www.domain.com", ".com.") = "www.domain.com"
        removeEnd("www.domain.com", ".com") = "www.domain"
        removeEnd("www.domain.com", "domain") = "www.domain.com"
        removeEnd("abc", "") = "abc"

        Parameters:
        str - the source String to search, may be null
        remove - the String to search for and remove, may be null
        Returns:
        the String to search for and remove, may be null
      • lowerCase

        public static String lowerCase​(String str)
        Converts a String to lower case

        A null input String returns null.

        lowerCase(null) = null
        lowerCase("") = ""
        lowerCase("aBc") = "abc"

        Parameters:
        str - the String to lower case, may be null
        Returns:
        the lower cased String, null if null String input
      • upperCase

        public static String upperCase​(String str)
        Converts a String to upper case

        A null input String returns null.

        upperCase(null) = null
        upperCase("") = ""
        upperCase("aBc") = "ABC"

        Parameters:
        str - the String to upper case, may be null
        Returns:
        the upper cased String, null if null String input
      • replace

        public static String replace​(String str,
                                     String searchString,
                                     String replacement)
        Replaces all occurrences of a String within another String

        A null reference passed to this method is a no-op.

        replace(null, *, *) = null
        replace("", *, *) = ""
        replace("any", null, *) = "any"
        replace("any", *, null) = "any"
        replace("any", "", *) = "any"
        replace("aba", "a", null) = "aba"
        replace("aba", "a", "") = "b"
        replace("aba", "a", "z") = "zbz"

        Parameters:
        str - text to search and replace in, may be null
        searchString - the String to search for, may be null
        replacement - the String to replace it with, may be null
        Returns:
        the text with any replacements processed, null if null String input
      • replace

        public static String replace​(String str,
                                     String searchString,
                                     String replacement,
                                     int max)
        Replaces a String with another String inside a larger String, for the first max values of the search String.

        A null reference passed to this method is a no-op.

        replace(null, *, *, *) = null
        replace("", *, *, *) = ""
        replace("any", null, *, *) = "any"
        replace("any", *, null, *) = "any"
        replace("any", "", *, *) = "any"
        replace("any", *, *, 0) = "any"
        replace("abaa", "a", null, -1) = "abaa"
        replace("abaa", "a", "", -1) = "b"
        replace("abaa", "a", "z", 0) = "abaa"
        replace("abaa", "a", "z", 1) = "zbaa"
        replace("abaa", "a", "z", 2) = "zbza"
        replace("abaa", "a", "z", -1) = "zbzz"

        Parameters:
        str - text to search and replace in, may be null
        searchString - the String to search for, may be null
        replacement - the String to replace it with, may be null
        max - maximum number of values to replace, or -1 if no maximum
        Returns:
        the text with any replacements processed, null if null String input
      • concatenate

        public static String concatenate​(Object... objects)
      • like

        public static boolean like​(String str,
                                   String pattern)
        Checks if the String is matched using given Pattern.

        Syntax:

         # - matches any single digit
         ? - matches any single character
         * - matches any character 0 or more times
         @ - matches any single alphabetic character
         [charlist] - matches any single character in charlist
         [!charlist]- matches any single character not in charlist
         X+ - matches @{code X} one or more times
         

        Examples:

        like(null, "") = true
        like("", "#") = false
        like("9", "#") = true
        like("a", "?") = true
        like("a", "@") = true
        like("1a23", "*") = true
        like("foo@bar.com", "?+\@?+\.?+") = true
        like("+38(099) 123-12-12", "+##(###) ###-##-##") = true
        Parameters:
        str - any String
        pattern - pattern
        Returns:
        true if the String matches given pattern
      • textJoin

        public static String textJoin​(String delimiter,
                                      Object... values)
        Joins an array of objects by the given delimiter to the single string. Blank strings will be ignored.

        Examples:

        textJoin(null, null) = null
        textJoin(",", null) = null
        textJoin(",", []) = ""
        textJoin(null, ["a,b,c"]) = "a,b,c"
        textJoin(", ", ["a", "b", "c"]) = "a, b, c"
        textJoin(",", ["a", "", "b", null, "c"]) = "a, b, c"
        Parameters:
        delimiter - the delimiting string
        values - an array of objects to join by target delimiter
        Returns:
        the single string computing by joining of each array element
      • textSplit

        public static String[] textSplit​(String delimiter,
                                         String str)
        Splits string by given delimiter.

        Examples:

        textSplit(null, null) = null
        textSplit(",", null) = null
        textSplit(null, "a,b,c") = ["a,b,c"]
        textSplit(",", "a,b,c") = ["a", "b", "c"]
        textSplit(",", ",,a,,b,,c,,") = ["a", "b", "c"]
        Parameters:
        delimiter - the delimiting string
        str - string to split
        Returns:
        the array of strings computing by splitting target string by given delimiter
      • format

        public static String format​(String pattern,
                                    Object... args)
        Format string with given pattern and the given arguments.

        Examples:
         format(null) = null
         format("")   = ""
         format("Hello, {0}!", "John") = "Hello, John!"
         format("{0}, {1}!", "Good evening", "John") = "Good evening, John!"
         
        Parameters:
        pattern - pattern
        args - object(s) to format
        Returns:
        formatted string
        See Also:
        MessageFormat
      • toLocale

        public static Locale toLocale​(String localeStr)
        Convert string locale to Locale
        Parameters:
        localeStr - locale as string
        Returns:
        parsed Locale object