public class StringUtils extends Object
Constructor and Description |
---|
StringUtils() |
Modifier and Type | Method and Description |
---|---|
static List<String> |
asList(Iterator<String> it)
Converts a Iterator<String>
|
static String |
asString(Iterator<String> it,
String divider)
Returns a string which is the concatination of an iterator of strings with the divider in between.
|
static String |
asString(List<String> list)
Returns a string, which is all the Strings in the list, with "\n" between the strings.
|
static String |
asString(List<String> list,
String divider)
Returns a list of Strings and appends them to one list, with the divider in between.
|
static String |
asString(String[] stringArray,
String divider)
Returns a string which is the concatination of an array of strings with the divider in between.
|
static StringList |
asStringList(Iterator<String> it)
Converts a Iterator of Strings to a StringList.
|
static String |
bite(String s,
int maxLength,
int maxLineLength,
String lineDivider,
String maxLengthExceededIndicator)
Split a string in to bites divide in certain maxLineLength by "lineDivider".
|
static String |
bite(String s,
int maxLineLength,
String lineDivider)
Split a string in to bites divide in certain lengths by "lineDivider".
|
static String |
capitalize(String s)
The following method converts all the letters into upper/lower case, depending on their position near a space or other special chars.
|
static boolean |
doArrayContains(String[] array,
String value,
boolean ignoreCase)
Determines if a String value is contained in a String array.
|
static String |
escapeHtml(String s)
Escapes a characters in string to be HTML safe.
|
protected static String |
getCaseInsensitiveEnvironmentProperty(String key)
Finds a system env with the key value.
|
protected static String |
getCaseInsensitiveSystemProperty(String key)
Finds a system property with the key value.
|
static boolean |
isEmpty(String s)
Will determine if a String contains an empty value.
|
static String |
leftPad(String value,
char pad,
int length)
Left pads the a string with a char until has a certain length.
|
static String |
leftPad(String value,
int length)
Left pads the a string with space until has a certain length.
|
static String |
removeValueQuotes(String value)
If a string value starts and ends with either " (Quotation Mark) or ' (Apostrophe).
|
static String |
repeat(String s,
int times)
Repeat a string N times with linefeed "\\n" as divider in between.
|
static String |
repeat(String s,
String divider,
int times)
Repeat a string N times with a divider in between.
|
static String |
replaceEnvironmentProperties(String value)
Returns a converted string where all "
${name} " is replace with the corresponding env properties value, if the name is a valid env properties name. |
static String |
replaceInlinedProperties(String value)
Calls the two methods replaceSystemProperties(String) and replaceEnvironmentProperties(String).
|
static String |
replaceSystemProperties(String value)
Returns a converted string where all "
${name} " is replace with the corresponding system properties value, if the name is a valid system properties name. |
static String |
rightPad(String value,
char pad,
int length)
Right pads the a string with a char until has a certain length.
|
static String |
rightPad(String value,
int length)
Right pads the a string with a space until has a certain length.
|
static String |
shuffle(String s)
Shuffle the characters in string using the Collection.shuffle() method.
|
static void |
validateString(String propertyName,
String name,
String value)
Convenience method which validate a string null, empty after trim (length==0)
|
static void |
validateString(String propertyName,
String name,
String value,
boolean allowNull,
boolean allowEmpty)
Convenience method which validate a string null, empty after trim (length==0)
|
public static final List<String> asList(Iterator<String> it)
Iterator<String> into a sorted List
.
- Parameters:
it
- the iterator
- Returns:
- a sorted list of strings
-
asStringList
public static final StringList asStringList(Iterator<String> it)
Converts a Iterator of Strings to a StringList.
- Parameters:
it
- the Iterator of Strings
- Returns:
- a StringList with the strings in the iterator, sorted.
- Since:
- 2.0
-
isEmpty
public static final boolean isEmpty(String s)
Will determine if a String contains an empty value.
- Parameters:
s
- the string
- Returns:
true
if the string is either null
or has a length of zero after a trim, otherwise false
.
-
asString
public static final String asString(List<String> list)
Returns a string, which is all the Strings in the list, with "\n" between the strings.
- Parameters:
list
- the list of strings
- Returns:
- a string of all the strings with "\n" in between.
-
asString
public static final String asString(List<String> list,
String divider)
Returns a list of Strings and appends them to one list, with the divider in between.
- Parameters:
list
- the list of strings
divider
- the divider.
- Returns:
- a string with with all the strings, with the divider in between.
-
asString
public static final String asString(Iterator<String> it,
String divider)
Returns a string which is the concatination of an iterator of strings with the divider in between.
- Parameters:
it
- the iterator of string
divider
- the string divider.
- Returns:
- a string of all the strings in the iterator seperated by the divider.
-
asString
public static final String asString(String[] stringArray,
String divider)
Returns a string which is the concatination of an array of strings with the divider in between.
- Parameters:
stringArray
- the array of strings
divider
- the divider.
- Returns:
- a string with with all the strings, with the divider in between.
-
replaceInlinedProperties
public static final String replaceInlinedProperties(String value)
Calls the two methods replaceSystemProperties(String) and replaceEnvironmentProperties(String).
This method will only convert the string if StaticPropertyFrameworkConfiguration.doReplaceInlinedProperties()
is true
.
- Parameters:
value
- the value to be preparsed
- Returns:
- a method where all "${name}" is replace be the value of System.properties and System.env, if found and if
StaticPropertyFrameworkConfiguration.doReplaceInlinedProperties()
is true
.
- See Also:
PropertyFrameworkGlobals
,
replaceSystemProperties(String)
,
replaceEnvironmentProperties(String)
-
removeValueQuotes
public static final String removeValueQuotes(String value)
If a string value starts and ends with either " (Quotation Mark) or ' (Apostrophe).
Than can it be removed if PropertyFrameworkGlobals.isDisardValueQuotes()
is true
.
- Parameters:
value
- the string value
- Returns:
- a value where Quotation Mark or Apostrophe is removed, if they are the first and the ladt character in the string.
- Since:
- 1.8
-
replaceSystemProperties
public static final String replaceSystemProperties(String value)
Returns a converted string where all "${name}
" is replace with the corresponding system properties value, if the name is a valid system properties name.
This method will only convert the string if StaticPropertyFrameworkConfiguration.doReplaceInlinedProperties()
is true
.
Important:This is NOT case sensitive.
- Parameters:
value
- the value to be converted
- Returns:
- a string where all "
${name}
" is replaced with System.properties value, if that value is not null
.
- See Also:
System.getProperty(String)
,
System.getProperties()
,
PropertyFrameworkGlobals
-
replaceEnvironmentProperties
public static final String replaceEnvironmentProperties(String value)
Returns a converted string where all "${name}
" is replace with the corresponding env properties value, if the name is a valid env properties name.
This method will only convert the string if StaticPropertyFrameworkConfiguration.doReplaceInlinedProperties()
is true
.
Important:This is NOT case sensitive.
- Parameters:
value
- the value to be converted
- Returns:
- a string where all "
${name}
" is replaced with System.getenv() value, if that value is not null
.
- See Also:
System.getenv()
,
System.getenv(String)
,
PropertyFrameworkGlobals
-
getCaseInsensitiveSystemProperty
protected static String getCaseInsensitiveSystemProperty(String key)
Finds a system property with the key value.
- Parameters:
key
- the key to look for in System.getProperty(String)
.
- Returns:
- return the value in the
System.getProperty(String)
which has the "key", case insensive, otherwise returns null
.
- See Also:
System.getProperty(String)
-
getCaseInsensitiveEnvironmentProperty
protected static String getCaseInsensitiveEnvironmentProperty(String key)
Finds a system env with the key value.
- Parameters:
key
- the key to look for in System.getenv(String)
.
- Returns:
- return the value in the
System.getenv(String)
which has the "key", case insensive, otherwise returns null
.
- See Also:
System.getenv(String)
-
doArrayContains
public static final boolean doArrayContains(String[] array,
String value,
boolean ignoreCase)
Determines if a String value is contained in a String array.
- Parameters:
array
- the String array.
value
- the String value.
ignoreCase
- when comparing strnig, shall case be ignored.
- Returns:
false
if array is null
or empty, or value is null
or not in array, otherwise true
.
-
shuffle
public static final String shuffle(String s)
throws NullPointerException
Shuffle the characters in string using the Collection.shuffle() method.
- Parameters:
s
- the string which characters we want to shuffle around.
- Returns:
- the shuffled string.
- Throws:
NullPointerException
- if the string is null
.
- See Also:
Collections.shuffle(List)
-
validateString
public static final void validateString(String propertyName,
String name,
String value)
throws PropertyException
Convenience method which validate a string null, empty after trim (length==0)
- Parameters:
propertyName
- the name of property
name
- the name of string validated.
value
- the string value.
- Throws:
PropertyException
- if vaue is either null
or zero length after trim.
-
validateString
public static final void validateString(String propertyName,
String name,
String value,
boolean allowNull,
boolean allowEmpty)
throws PropertyException
Convenience method which validate a string null, empty after trim (length==0)
- Parameters:
propertyName
- the name of property
name
- the name of string validated.
value
- the string value.
allowNull
- is null
allowed.
allowEmpty
- is empty after trim allowed.
- Throws:
PropertyException
- if the value is null
and its not allowed, or the value is empty after trim and it is not allowed.
-
leftPad
public static String leftPad(String value,
int length)
Left pads the a string with space until has a certain length.
- Parameters:
value
- the string value to pad
length
- the desired length of the return string value
- Returns:
- if
null
, left pad values until length, if the string value length is greater than the desired length the value is returned, otherwise the pad value if padded on the left until the desired length is reached.
-
leftPad
public static String leftPad(String value,
char pad,
int length)
Left pads the a string with a char until has a certain length.
- Parameters:
value
- the string value to pad
pad
- the char value to left pad with, if null
or empty a white space is used.
length
- the desired length of the return string value
- Returns:
- if
null
, left pad values until length, if the string value length is greater than the desired length the value is returned, otherwise the pad value if padded on the left until the desired length is reached.
-
rightPad
public static String rightPad(String value,
int length)
Right pads the a string with a space until has a certain length.
- Parameters:
value
- the string value to pad
length
- the desired length of the return string value
- Returns:
- if
null
, left pad values until length, if the string value length is greater than the desired length the value is returned, otherwise the pad value if padded on the right until the desired length is reached.
-
rightPad
public static String rightPad(String value,
char pad,
int length)
Right pads the a string with a char until has a certain length.
- Parameters:
value
- the string value to pad
pad
- the char value to right pad with, if null
or empty a white space is used.
length
- the desired length of the return string value
- Returns:
- if
null
, left pad values until length, if the string value length is greater than the desired length the value is returned, otherwise the pad value if padded on the right until the desired length is reached.
-
escapeHtml
public static String escapeHtml(String s)
Escapes a characters in string to be HTML safe.
- Parameters:
s
- the string
- Returns:
- if either a char is above 127 ISO 8859-1 Position or is either '"', '<', '>' or '&' they are converted to the number characters entities '&#<char number>'.
-
repeat
public static String repeat(String s,
int times)
Repeat a string N times with linefeed "\\n" as divider in between.
- Parameters:
s
- the string to repeat
times
- how many times the string should be repeated.
- Returns:
- the string s repeated N times with \\n in between.
-
repeat
public static String repeat(String s,
String divider,
int times)
Repeat a string N times with a divider in between.
- Parameters:
s
- the string to repeat
divider
- the divider between the repeat and as last. if null
then ignored.
times
- how many times the string should be repeated.
- Returns:
- the string s repeated N times with the divider in between.
-
bite
public static String bite(String s,
int maxLength,
int maxLineLength,
String lineDivider,
String maxLengthExceededIndicator)
Split a string in to bites divide in certain maxLineLength by "lineDivider". If the string is longer than maxLength than it is cut to this length and
maxLengthExceededIndicator is added if it is not null
.
- Parameters:
s
- the string
maxLength
- the total max length of the string.
maxLineLength
- the max length of each section which should be seperate by a "lineDivider".
lineDivider
- the line divider betweeen two sections of the string of "maxLineLength", if null
than not used.
maxLengthExceededIndicator
- append on the end of string to indicate that more was/is present, could be "..." or "...(more)", if null
than ignored.
- Returns:
- if s is
null
then null
is returned, otherwise see method description.
- See Also:
bite(String, int, String)
-
bite
public static String bite(String s,
int maxLineLength,
String lineDivider)
Split a string in to bites divide in certain lengths by "lineDivider".
- Parameters:
s
- the string to divide.
maxLineLength
- the max length of each line.
lineDivider
- the line divider, if null
, than not used.
- Returns:
- a string divide by "lineDivider" until it is below length.
-
capitalize
public static String capitalize(String s)
The following method converts all the letters into upper/lower case, depending on their position near a space or other special chars.
- Parameters:
s
- the string to capatilize.
- Returns:
- all characters is lowercased, except if it is the first char, or the next characters is whitespace, dot or backslash.
- Since:
- 2.0
- See Also:
Character.isWhitespace(char)