com.github.drinkjava2.jdialects
Class StrUtils

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

public class StrUtils
extends Object

String Utilities

Since:
1.0.0
Author:
Yong Zhu

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 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 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 isEmpty(Object str)
          Check whether the given String is empty.
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 replaceIgnoreCase(String str, String findtxt, String replacetxt)
          Replace all sub strings ignore case
replaceIgnoreCase("AbcDECd", "Cd", "FF") = "AbFFEFF"
static boolean startsWithIgnoreCase(String str, String prefix)
          Test if the given String starts with the specified prefix, ignoring upper/lower case.
static String substringBetween(String str, String open, String close)
          Gets the String that is nested in between two Strings.
static List<String> substringsBetween(String txt, String tag)
          Return all sub-Strings between 1 tags
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
 

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

replaceIgnoreCase

public static String replaceIgnoreCase(String str,
                                       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


substringsBetween

public static List<String> substringsBetween(String txt,
                                             String tag)
Return all sub-Strings between 1 tags


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

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

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



Copyright © 2017. All rights reserved.