com.github.drinkjava2.jdialects
Class StrUtils

java.lang.Object
  extended by com.github.drinkjava2.jdialects.StrUtils

public class StrUtils
extends Object

String Utilities usually used inside of framework

Since:
1.0.0
Author:
Yong Zhu

Field Summary
static String WHITESPACE
           
 
Method Summary
static boolean arraysEqual(Object[] array1, Object[] array2)
          Compare 2 array
static String arrayToString(Object[] array)
          Change a Object array to "obj1,obj2...,objn" String
static String arrayToString(Object[] array, String seperateString)
          Change a Object array to connected string by given seperateString
static String arrayToStringButSkipFirst(Object[] array)
          Change a Object array to "obj1,obj2...,objn" String
static boolean containsIgnoreCase(String str, String searchStr)
          Find if exist searchStr in str ignore case
static boolean containsWhitespace(CharSequence str)
          Check whether the given CharSequence contains any whitespace characters.
static boolean containsWhitespace(String str)
          Check whether the given String contains any whitespace characters.
static int countMatches(CharSequence str, char ch)
           Counts how many times the char appears in the given string.
static String formatSQL(String sql)
          Format all " ", \t, \r...
static String getRandomString(int length)
           
static boolean hasLength(CharSequence str)
          Check that the given CharSequence is neither null nor of length 0.
static boolean hasLength(String str)
          Check that the given String is neither null nor of length 0.
static int indexOfIgnoreCase(String str, String searchStr)
          Return first postion ignore case, return -1 if not found
static boolean isCapitalizedString(String str)
          Return true if first letter is Capitalised
static boolean isEmpty(Object str)
          Check whether the given String is empty.
static boolean isInvisibleChar(char c)
          Return true if is an invisible Char like space, tab, return...
static boolean isNormalLetters(char c)
          Check if a String only have a-z,A-Z,0-9,"_" characters
static String[] joinStringArray(String[] array1, String[] array2)
          Join 2 String array into one
static int lastIndexOfIgnoreCase(String str, String searchStr)
          Return last sub-String position ignore case, return -1 if not found
static String listToString(List<?> lst)
          Change a Object List to "obj1,obj2...,objn" String
static String replace(String originString, String oldPattern, String newPattern)
          Replace all occurrences of a substring within a string with another string.
static String replaceFirst(String originString, String oldPattern, String newPattern)
          Replace first occurrences of a substring within a string with another string.
static String replaceIgnoreCase(String text, String findtxt, String replacetxt)
          Replace all sub strings ignore case
replaceIgnoreCase("AbcDECd", "Cd", "FF") = "AbFFEFF"
static String[] split(String str, char separatorChar)
           Splits the provided text into an array, separator specified.
static String[] split(String separators, String list)
           
static String[] split(String separators, String list, boolean include)
           
static boolean startsWithIgnoreCase(String str, String prefix)
          Test if the given String starts with the specified prefix, ignoring upper/lower case.
static String substringAfter(String str, String separator)
           Gets the substring after the first occurrence of a separator.
static String substringAfterLast(String str, String separator)
           Gets the substring after the last occurrence of a separator.
static String substringBefore(String str, String separator)
           Gets the substring before the first occurrence of a separator.
static String substringBeforeLast(String str, String separator)
           Gets the substring before the last occurrence of a separator.
static String substringBetween(String str, String open, String close)
          Gets the String that is nested in between two Strings.
static String[] substringsBetween(String str, String open, String close)
           Searches a String for substrings delimited by a start and end tag, returning all matching substrings in an array.
static String toLowerCaseFirstOne(String s)
          First letter change to lower
static String toUpperCaseFirstOne(String s)
          First letter change to capitalised
static String trimAllWhitespace(String str)
          Trim all whitespace from the given String: leading, trailing, and in between characters.
static String trimLeadingCharacter(String str, char leadingCharacter)
          Trim all occurrences of the supplied leading character from the given String.
static String trimLeadingWhitespace(String str)
          Trim leading whitespace from the given String.
static String trimTrailingCharacter(String str, char trailingCharacter)
          Trim all occurrences of the supplied trailing character from the given String.
static String trimTrailingWhitespace(String str)
          Trim trailing whitespace from the given String.
static String trimWhitespace(String str)
          Trim leading and trailing whitespace from the given String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WHITESPACE

public static final String WHITESPACE
See Also:
Constant Field Values
Method Detail

isEmpty

public static boolean isEmpty(Object str)
Check whether the given String is empty.


hasLength

public static boolean hasLength(CharSequence str)
Check that the given CharSequence is neither null nor of length 0. Note: Will return true for a CharSequence that purely consists of whitespace.

 StrUtils.hasLength(null) = false
 StrUtils.hasLength("") = false
 StrUtils.hasLength(" ") = true
 StrUtils.hasLength("Hello") = true
 

Parameters:
str - the CharSequence to check (may be null)
Returns:
true if the CharSequence is not null and has length
See Also:
#hasText(String)

hasLength

public static boolean hasLength(String str)
Check that the given String is neither null nor of length 0. Note: Will return true for a String that purely consists of whitespace.

Parameters:
str - the String to check (may be null)
Returns:
true if the String is not null and has length
See Also:
hasLength(CharSequence)

containsWhitespace

public static boolean containsWhitespace(CharSequence str)
Check whether the given CharSequence contains any whitespace characters.

Parameters:
str - the CharSequence to check (may be null)
Returns:
true if the CharSequence is not empty and contains at least 1 whitespace character
See Also:
Character.isWhitespace(char)

containsWhitespace

public static boolean containsWhitespace(String str)
Check whether the given String contains any whitespace characters.

Parameters:
str - the String to check (may be null)
Returns:
true if the String is not empty and contains at least 1 whitespace character
See Also:
containsWhitespace(CharSequence)

trimWhitespace

public static String trimWhitespace(String str)
Trim leading and trailing whitespace from the given String.

Parameters:
str - the String to check
Returns:
the trimmed String
See Also:
Character.isWhitespace(char)

trimAllWhitespace

public static String trimAllWhitespace(String str)
Trim all whitespace from the given String: leading, trailing, and in between characters.

Parameters:
str - the String to check
Returns:
the trimmed String
See Also:
Character.isWhitespace(char)

trimLeadingWhitespace

public static String trimLeadingWhitespace(String str)
Trim leading whitespace from the given String.

Parameters:
str - the String to check
Returns:
the trimmed String
See Also:
Character.isWhitespace(char)

trimTrailingWhitespace

public static String trimTrailingWhitespace(String str)
Trim trailing whitespace from the given String.

Parameters:
str - the String to check
Returns:
the trimmed String
See Also:
Character.isWhitespace(char)

trimLeadingCharacter

public static String trimLeadingCharacter(String str,
                                          char leadingCharacter)
Trim all occurrences of the supplied leading character from the given String.

Parameters:
str - the String to check
leadingCharacter - the leading character to be trimmed
Returns:
the trimmed String

trimTrailingCharacter

public static String trimTrailingCharacter(String str,
                                           char trailingCharacter)
Trim all occurrences of the supplied trailing character from the given String.

Parameters:
str - the String to check
trailingCharacter - the trailing character to be trimmed
Returns:
the trimmed String

startsWithIgnoreCase

public static boolean startsWithIgnoreCase(String str,
                                           String prefix)
Test if the given String starts with the specified prefix, ignoring upper/lower case.

Parameters:
str - the String to check
prefix - the prefix to look for
See Also:
String.startsWith(java.lang.String, int)

containsIgnoreCase

public static boolean containsIgnoreCase(String str,
                                         String searchStr)
Find if exist searchStr in str ignore case


replace

public static String replace(String originString,
                             String oldPattern,
                             String newPattern)
Replace all occurrences of a substring within a string with another string.

Parameters:
originString - The original String
oldPattern - old String Pattern to replace
newPattern - new String pattern to insert
Returns:
a String with the replacements

replaceFirst

public static String replaceFirst(String originString,
                                  String oldPattern,
                                  String newPattern)
Replace first occurrences of a substring within a string with another string.

Parameters:
originString - The original String
oldPattern - old String Pattern to replace
newPattern - new String pattern to insert
Returns:
a String with the replacements

replaceIgnoreCase

public static String replaceIgnoreCase(String text,
                                       String findtxt,
                                       String replacetxt)
Replace all sub strings ignore case
replaceIgnoreCase("AbcDECd", "Cd", "FF") = "AbFFEFF"


indexOfIgnoreCase

public static int indexOfIgnoreCase(String str,
                                    String searchStr)
Return first postion ignore case, return -1 if not found


lastIndexOfIgnoreCase

public static int lastIndexOfIgnoreCase(String str,
                                        String searchStr)
Return last sub-String position ignore case, return -1 if not found


split

public static String[] split(String str,
                             char separatorChar)

Splits the provided text into an array, separator specified. This is an alternative to using StringTokenizer.

The separator is not included in the returned String array. Adjacent separators are treated as one separator. For more control over the split use the StrTokenizer class.

A null input String returns null.

 StringUtils.split(null, *)         = null
 StringUtils.split("", *)           = []
 StringUtils.split("a.b.c", '.')    = ["a", "b", "c"]
 StringUtils.split("a..b.c", '.')   = ["a", "b", "c"]
 StringUtils.split("a:b:c", '.')    = ["a:b:c"]
 StringUtils.split("a b c", ' ')    = ["a", "b", "c"]
 

Parameters:
str - the String to parse, may be null
separatorChar - the character used as the delimiter
Returns:
an array of parsed Strings, null if null String input
Since:
2.0

split

public static String[] split(String separators,
                             String list)

split

public static String[] split(String separators,
                             String list,
                             boolean include)

substringsBetween

public static String[] substringsBetween(String str,
                                         String open,
                                         String close)

Searches a String for substrings delimited by a start and end tag, returning all matching substrings in an array.

A null input String returns null. A null open/close returns null (no match). An empty ("") open/close returns null (no match).

 StringUtils.substringsBetween("[a][b][c]", "[", "]") = ["a","b","c"]
 StringUtils.substringsBetween(null, *, *)            = null
 StringUtils.substringsBetween(*, null, *)            = null
 StringUtils.substringsBetween(*, *, null)            = null
 StringUtils.substringsBetween("", "[", "]")          = []
 

Parameters:
str - the String containing the substrings, null returns null, empty returns empty
open - the String identifying the start of the substring, empty returns null
close - the String identifying the end of the substring, empty returns null
Returns:
a String Array of substrings, or null if no match
Since:
2.3

substringBetween

public static String substringBetween(String str,
                                      String open,
                                      String close)
Gets the String that is nested in between two Strings. Only the first match is returned. A null input String returns null. A null open/close returns null (no match). An empty ("") open and close returns an empty string.
 StringUtils.substringBetween("wx[b]yz", "[", "]") = "b"
 StringUtils.substringBetween(null, *, *)          = null
 StringUtils.substringBetween(*, null, *)          = null
 StringUtils.substringBetween(*, *, null)          = null
 StringUtils.substringBetween("", "", "")          = ""
 StringUtils.substringBetween("", "", "]")         = null
 StringUtils.substringBetween("", "[", "]")        = null
 StringUtils.substringBetween("yabcz", "", "")     = ""
 StringUtils.substringBetween("yabcz", "y", "z")   = "abc"
 StringUtils.substringBetween("yabczyabcz", "y", "z")   = "abc"
 

Parameters:
str - the String containing the substring, may be null
open - the String before the substring, may be null
close - the String after the substring, may be null
Returns:
the substring, null if no match
Since:
2.0

substringBefore

public static String substringBefore(String str,
                                     String separator)

Gets the substring before the first occurrence of a separator. The separator is not returned.

A null string input will return null. An empty ("") string input will return the empty string. A null separator will return the input string.

If nothing is found, the string input is returned.

 StringUtils.substringBefore(null, *)      = null
 StringUtils.substringBefore("", *)        = ""
 StringUtils.substringBefore("abc", "a")   = ""
 StringUtils.substringBefore("abcba", "b") = "a"
 StringUtils.substringBefore("abc", "c")   = "ab"
 StringUtils.substringBefore("abc", "d")   = "abc"
 StringUtils.substringBefore("abc", "")    = ""
 StringUtils.substringBefore("abc", null)  = "abc"
 

Parameters:
str - the String to get a substring from, may be null
separator - the String to search for, may be null
Returns:
the substring before the first occurrence of the separator, null if null String input

substringAfter

public static String substringAfter(String str,
                                    String separator)

Gets the substring after the first occurrence of a separator. The separator is not returned.

A null string input will return null. An empty ("") string input will return the empty string. A null separator will return the empty string if the input string is not null.

If nothing is found, the empty string is returned.

 StringUtils.substringAfter(null, *)      = null
 StringUtils.substringAfter("", *)        = ""
 StringUtils.substringAfter(*, null)      = ""
 StringUtils.substringAfter("abc", "a")   = "bc"
 StringUtils.substringAfter("abcba", "b") = "cba"
 StringUtils.substringAfter("abc", "c")   = ""
 StringUtils.substringAfter("abc", "d")   = ""
 StringUtils.substringAfter("abc", "")    = "abc"
 

Parameters:
str - the String to get a substring from, may be null
separator - the String to search for, may be null
Returns:
the substring after the first occurrence of the separator, null if null String input

substringAfterLast

public static String substringAfterLast(String str,
                                        String separator)

Gets the substring after the last occurrence of a separator. The separator is not returned.

A null string input will return null. An empty ("") string input will return the empty string. An empty or null separator will return the empty string if the input string is not null.

If nothing is found, the empty string is returned.

 StringUtils.substringAfterLast(null, *)      = null
 StringUtils.substringAfterLast("", *)        = ""
 StringUtils.substringAfterLast(*, "")        = ""
 StringUtils.substringAfterLast(*, null)      = ""
 StringUtils.substringAfterLast("abc", "a")   = "bc"
 StringUtils.substringAfterLast("abcba", "b") = "a"
 StringUtils.substringAfterLast("abc", "c")   = ""
 StringUtils.substringAfterLast("a", "a")     = ""
 StringUtils.substringAfterLast("a", "z")     = ""
 

Parameters:
str - the String to get a substring from, may be null
separator - the String to search for, may be null
Returns:
the substring after the last occurrence of the separator, null if null String input
Since:
2.0

substringBeforeLast

public static String substringBeforeLast(String str,
                                         String separator)

Gets the substring before the last occurrence of a separator. The separator is not returned.

A null string input will return null. An empty ("") string input will return the empty string. An empty or null separator will return the input string.

If nothing is found, the string input is returned.

 StringUtils.substringBeforeLast(null, *)      = null
 StringUtils.substringBeforeLast("", *)        = ""
 StringUtils.substringBeforeLast("abcba", "b") = "abc"
 StringUtils.substringBeforeLast("abc", "c")   = "ab"
 StringUtils.substringBeforeLast("a", "a")     = ""
 StringUtils.substringBeforeLast("a", "z")     = "a"
 StringUtils.substringBeforeLast("a", null)    = "a"
 StringUtils.substringBeforeLast("a", "")      = "a"
 

Parameters:
str - the String to get a substring from, may be null
separator - the String to search for, may be null
Returns:
the substring before the last occurrence of the separator, null if null String input
Since:
2.0

countMatches

public static int countMatches(CharSequence str,
                               char ch)

Counts how many times the char appears in the given string.

A null or empty ("") String input returns 0.

 StringUtils.countMatches(null, *)       = 0
 StringUtils.countMatches("", *)         = 0
 StringUtils.countMatches("abba", 0)  = 0
 StringUtils.countMatches("abba", 'a')   = 2
 StringUtils.countMatches("abba", 'b')  = 2
 StringUtils.countMatches("abba", 'x') = 0
 

Parameters:
str - the CharSequence to check, may be null
ch - the char to count
Returns:
the number of occurrences, 0 if the CharSequence is null
Since:
3.4

getRandomString

public static String getRandomString(int length)

arraysEqual

public static boolean arraysEqual(Object[] array1,
                                  Object[] array2)
Compare 2 array

Returns:
true if each item equal

arrayToStringButSkipFirst

public static String arrayToStringButSkipFirst(Object[] array)
Change a Object array to "obj1,obj2...,objn" String


arrayToString

public static String arrayToString(Object[] array)
Change a Object array to "obj1,obj2...,objn" String


arrayToString

public static String arrayToString(Object[] array,
                                   String seperateString)
Change a Object array to connected string by given seperateString


listToString

public static String listToString(List<?> lst)
Change a Object List to "obj1,obj2...,objn" String


joinStringArray

public static String[] joinStringArray(String[] array1,
                                       String[] array2)
Join 2 String array into one


isCapitalizedString

public static boolean isCapitalizedString(String str)
Return true if first letter is Capitalised


toLowerCaseFirstOne

public static String toLowerCaseFirstOne(String s)
First letter change to lower


toUpperCaseFirstOne

public static String toUpperCaseFirstOne(String s)
First letter change to capitalised


isNormalLetters

public static boolean isNormalLetters(char c)
Check if a String only have a-z,A-Z,0-9,"_" characters


isInvisibleChar

public static boolean isInvisibleChar(char c)
Return true if is an invisible Char like space, tab, return... char


formatSQL

public static String formatSQL(String sql)
Format all " ", \t, \r... , to " ",



Copyright © 2018. All rights reserved.