Package org.openl.rules.util
Class Strings
- java.lang.Object
-
- org.openl.rules.util.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 Stringconcatenate(Object... objects)static booleancontains(String str, char searchChar)Checks if String contains a search character, handlingnull.static booleancontains(String str, String searchStr)Checks if String contains a search String, handlingnull.static booleancontainsAny(String str, char... chars)Checks if the String contains any character in the given set of characters.static booleancontainsAny(String str, String searchChars)Checks if the String contains any character in the given set of characters.static booleanendsWith(String str, String suffix)Check if a String ends with a specified suffix.
Two null references are considered to be equal.static Stringformat(String pattern, Object... args)Format string with given pattern and the given arguments.
Examples:static booleanisEmpty(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 booleanisNotEmpty(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 intlength(String str)Returns the length of this string.static booleanlike(String str, String pattern)Checks if the String is matched using given Pattern.
Syntax:
static StringlowerCase(String str)Converts a String to lower case
A null input String returns null.static StringremoveEnd(String str, String remove)Removes a substring only if it is at the end of a source string, otherwise returns the source string.static StringremoveStart(String str, String remove)Removes a substring only if it is at the begining of a source string, otherwise returns the source string.static Stringreplace(String str, String searchString, String replacement)Replaces all occurrences of a String within another String
static Stringreplace(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 booleanstartsWith(String str, String prefix)Check if a String starts with a specified prefix.
Two null references are considered to be equal.static Stringsubstr(String str, int pos, int length)static Stringsubstring(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 Stringsubstring(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 StringtextJoin(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 DoubletoDouble(String str)static IntegertoInteger(String str)static LocaletoLocale(String localeStr)Convert string locale toLocalestatic NumbertoNumber(String str)static StringtoString(Object obj)static Stringtrim(String str)Returns a string whose value is this string, with any leading and trailing whitespace removed.static StringupperCase(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 usesString.indexOf(String).A
nullString will returnfalse.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 nullsearchStr- the String to find, may be null- Returns:
- true if the String contains the search String, false if not or
nullstring input
-
contains
public static boolean contains(String str, char searchChar)
Checks if String contains a search character, handling
null. This method usesString.indexOf(int).A
nullor empty ("") String will returnfalse.contains(null, *) = false contains("", *) = false contains("abc", 'a') = true contains("abc", 'z') = false- Parameters:
str- the String to check, may be nullsearchChar- the character to find- Returns:
- true if the String contains the search character, false if not or
nullstring input
-
containsAny
public static boolean containsAny(String str, char... chars)
Checks if the String contains any character in the given set of characters.
A
nullString will returnfalse. Anullor zero length search array will returnfalse.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 nullchars- the chars to search for, may be null- Returns:
- the
trueif any of the chars are found,falseif 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
nullString will returnfalse. Anullsearch string will returnfalse.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 nullsearchChars- the chars to search for, may be null- Returns:
- the
trueif any of the chars are found,falseif 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 nullprefix- 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 nullsuffix- the suffix to find, may be null- Returns:
- true if the String ends with the suffix, case sensitive, or both null
-
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 nullbeginIndex- 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 nullbeginIndex- the position to start from, negative means count back from the end of the String by this many charactersendIndex- 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 nullremove- 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 nullremove- 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 nullsearchString- the String to search for, may be nullreplacement- 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 nullsearchString- the String to search for, may be nullreplacement- the String to replace it with, may be nullmax- maximum number of values to replace, or -1 if no maximum- Returns:
- the text with any replacements processed, null if null String input
-
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 incharlistX+ - 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 Stringpattern- pattern- Returns:
trueif 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 stringvalues- 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 stringstr- 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) = nullformat("") = ""format("Hello, {0}!", "John") = "Hello, John!"format("{0}, {1}!", "Good evening", "John") = "Good evening, John!"- Parameters:
pattern- patternargs- object(s) to format- Returns:
- formatted string
- See Also:
MessageFormat
-
-