Class Strings
- java.lang.Object
-
- io.ultreia.java4all.lang.Strings
-
public class Strings extends Object
Created by tchemit on 29/12/2017.- Author:
- Tony Chemit - dev@tchemit.fr
-
-
Constructor Summary
Constructors Constructor Description Strings()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Stringcapitalize(String str)Capitalizes a String changing the first character to title case as perCharacter.toTitleCase(int).static Stringconvert(long value, double[] factors, String[] unites)Note: this method use the current locale (theLocale.getDefault()) in the methodMessageFormat(String).static StringconvertMemory(long value)Converts an memory measure into a human readable format.static StringconvertTime(long value)Converts an time delay into a human readable format.static StringconvertTime(long value, long value2)Converts an time period into a human readable format.static StringconvertToConstantName(String name)Convertir un nom en une constante Javastatic StringencodeSHA1(String toEncode)Convert a String to SHA1.static StringgetRelativeCamelCaseName(String packageName, String fullyQualifiedName, String... excludes)Get the relative camel case name for the givenfullyQualifiedName.static booleanisEmpty(String o)static booleanisNotEmpty(String o)static booleanisNumeric(String cs)Checks if the CharSequence contains only Unicode digits.static StringleftPad(String str, int size)Left pad a String with spaces (' ').static StringleftPad(String str, int size, char padChar)Left pad a String with a specified character.static StringleftPad(String str, int size, String padStr)Left pad a String with a specified String.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)static Stringrepeat(char ch, int repeat)Returns padding using the specified delimiter repeated to a given length.static StringrightPad(String str, int size, char padChar)Right pad a String with a specified character.static StringrightPad(String str, int size, String padStr)Right pad a String with a specified String.static String[]split(String str, String separatorChars)Splits the provided text into an array, separators specified.static StringsubstringAfter(String str, String separator)Gets the substring after the first occurrence of a separator.static StringsubstringBefore(String str, String separator)Gets the substring before the first occurrence of a separator.
-
-
-
Field Detail
-
EMPTY_STRING_ARRAY
public static final String[] EMPTY_STRING_ARRAY
-
SPACE
public static final String SPACE
A String for a space character.- Since:
- 3.2
- See Also:
- Constant Field Values
-
EMPTY
public static final String EMPTY
- See Also:
- Constant Field Values
-
-
Method Detail
-
encodeSHA1
public static String encodeSHA1(String toEncode)
Convert a String to SHA1.- Parameters:
toEncode- string to encode- Returns:
- sha1 corresponding
- Throws:
IllegalStateException- if could not found algorithm SHA1
-
convertTime
public static String convertTime(long value)
Converts an time delay into a human readable format.- Parameters:
value- the delay to convert- Returns:
- the memory representation of the given value
- See Also:
convert(long, double[], String[])
-
convertTime
public static String convertTime(long value, long value2)
Converts an time period into a human readable format.- Parameters:
value- the begin timevalue2- the end time- Returns:
- the time representation of the given value
- See Also:
convert(long, double[], String[])
-
convertMemory
public static String convertMemory(long value)
Converts an memory measure into a human readable format.- Parameters:
value- the memory measure to convert- Returns:
- the memory representation of the given value
- See Also:
convert(long, double[], String[])
-
convert
public static String convert(long value, double[] factors, String[] unites)
Note: this method use the current locale (theLocale.getDefault()) in the methodMessageFormat(String).- Parameters:
value- value to convertfactors- factors used form conversionunites- libelle of unites to use- Returns:
- the converted representation of the given value
-
convertToConstantName
public static String convertToConstantName(String name)
Convertir un nom en une constante JavaLes seuls caractères autorisés sont les alpha numériques, ains que l'underscore. tous les autres caractères seront ignorés.
- Parameters:
name- le nom à convertir- Returns:
- la constante générée
-
getRelativeCamelCaseName
public static String getRelativeCamelCaseName(String packageName, String fullyQualifiedName, String... excludes)
Get the relative camel case name for the givenfullyQualifiedName.first try to remove
packageName, then anyexcludeson starts, at last reduce with came case.- Parameters:
packageName- starts packagefullyQualifiedName- fqn to transformexcludes- optional starts packages to remove- Returns:
- the camel case result
-
leftPad
public static String leftPad(String str, int size, String padStr)
Left pad a String with a specified String.
Pad to a size of
size.Strings.leftPad(null, *, *) = null Strings.leftPad("", 3, "z") = "zzz" Strings.leftPad("bat", 3, "yz") = "bat" Strings.leftPad("bat", 5, "yz") = "yzbat" Strings.leftPad("bat", 8, "yz") = "yzyzybat" Strings.leftPad("bat", 1, "yz") = "bat" Strings.leftPad("bat", -1, "yz") = "bat" Strings.leftPad("bat", 5, null) = " bat" Strings.leftPad("bat", 5, "") = " bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadStr- the String to pad with, null or empty treated as single space- Returns:
- left padded String or original String if no padding is necessary,
nullif null String input
-
leftPad
public static String leftPad(String str, int size)
Left pad a String with spaces (' ').
The String is padded to the size of
size.Strings.leftPad(null, *) = null Strings.leftPad("", 3) = " " Strings.leftPad("bat", 3) = "bat" Strings.leftPad("bat", 5) = " bat" Strings.leftPad("bat", 1) = "bat" Strings.leftPad("bat", -1) = "bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad to- Returns:
- left padded String or original String if no padding is necessary,
nullif null String input
-
leftPad
public static String leftPad(String str, int size, char padChar)
Left pad a String with a specified character.
Pad to a size of
size.Strings.leftPad(null, *, *) = null Strings.leftPad("", 3, 'z') = "zzz" Strings.leftPad("bat", 3, 'z') = "bat" Strings.leftPad("bat", 5, 'z') = "zzbat" Strings.leftPad("bat", 1, 'z') = "bat" Strings.leftPad("bat", -1, 'z') = "bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadChar- the character to pad with- Returns:
- left padded String or original String if no padding is necessary,
nullif null String input
-
isEmpty
public static boolean isEmpty(String o)
-
isNotEmpty
public static boolean isNotEmpty(String o)
-
isNumeric
public static boolean isNumeric(String cs)
Checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false.
nullwill returnfalse. An empty CharSequence (length()=0) will returnfalse.Note that the method does not allow for a leading sign, either positive or negative. Also, if a String passes the numeric test, it may still generate a NumberFormatException when parsed by Integer.parseInt or Long.parseLong, e.g. if the value is outside the range for int or long respectively.
Strings.isNumeric(null) = false Strings.isNumeric("") = false Strings.isNumeric(" ") = false Strings.isNumeric("123") = true Strings.isNumeric("१२३") = true Strings.isNumeric("12 3") = false Strings.isNumeric("ab2c") = false Strings.isNumeric("12-3") = false Strings.isNumeric("12.3") = false Strings.isNumeric("-123") = false Strings.isNumeric("+123") = false- Parameters:
cs- the CharSequence to check, may be null- Returns:
trueif only contains digits, and is non-null
-
capitalize
public static String capitalize(String str)
Capitalizes a String changing the first character to title case as per
Character.toTitleCase(int). No other characters are changed.Strings.capitalize(null) = null Strings.capitalize("") = "" Strings.capitalize("cat") = "Cat" Strings.capitalize("cAt") = "CAt" Strings.capitalize("'cat'") = "'cat'"- Parameters:
str- the String to capitalize, may be null- Returns:
- the capitalized String,
nullif 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
nullsource string will returnnull. An empty ("") source string will return the empty string. Anullsearch string will return the source string.Strings.removeEnd(null, *) = null Strings.removeEnd("", *) = "" Strings.removeEnd(*, null) = * Strings.removeEnd("www.domain.com", ".com.") = "www.domain.com" Strings.removeEnd("www.domain.com", ".com") = "www.domain" Strings.removeEnd("www.domain.com", "domain") = "www.domain.com" Strings.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 substring with the string removed if found,
nullif null String input - Since:
- 2.1
-
rightPad
public static String rightPad(String str, int size, String padStr)
Right pad a String with a specified String.
The String is padded to the size of
size.Strings.rightPad(null, *, *) = null Strings.rightPad("", 3, "z") = "zzz" Strings.rightPad("bat", 3, "yz") = "bat" Strings.rightPad("bat", 5, "yz") = "batyz" Strings.rightPad("bat", 8, "yz") = "batyzyzy" Strings.rightPad("bat", 1, "yz") = "bat" Strings.rightPad("bat", -1, "yz") = "bat" Strings.rightPad("bat", 5, null) = "bat " Strings.rightPad("bat", 5, "") = "bat "- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadStr- the String to pad with, null or empty treated as single space- Returns:
- right padded String or original String if no padding is necessary,
nullif null String input
-
rightPad
public static String rightPad(String str, int size, char padChar)
Right pad a String with a specified character.
The String is padded to the size of
size.Strings.rightPad(null, *, *) = null Strings.rightPad("", 3, 'z') = "zzz" Strings.rightPad("bat", 3, 'z') = "bat" Strings.rightPad("bat", 5, 'z') = "batzz" Strings.rightPad("bat", 1, 'z') = "bat" Strings.rightPad("bat", -1, 'z') = "bat"- Parameters:
str- the String to pad out, may be nullsize- the size to pad topadChar- the character to pad with- Returns:
- right padded String or original String if no padding is necessary,
nullif 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
nullstring input will returnnull. An empty ("") string input will return the empty string. Anullseparator will return the empty string if the input string is notnull.If nothing is found, the empty string is returned.
Strings.substringAfter(null, *) = null Strings.substringAfter("", *) = "" Strings.substringAfter(*, null) = "" Strings.substringAfter("abc", "a") = "bc" Strings.substringAfter("abcba", "b") = "cba" Strings.substringAfter("abc", "c") = "" Strings.substringAfter("abc", "d") = "" Strings.substringAfter("abc", "") = "abc"- Parameters:
str- the String to get a substring from, may be nullseparator- the String to search for, may be null- Returns:
- the substring after the first occurrence of the separator,
nullif null String input
-
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
nullstring input will returnnull. An empty ("") string input will return the empty string. Anullseparator will return the input string.If nothing is found, the string input is returned.
Strings.substringBefore(null, *) = null Strings.substringBefore("", *) = "" Strings.substringBefore("abc", "a") = "" Strings.substringBefore("abcba", "b") = "a" Strings.substringBefore("abc", "c") = "ab" Strings.substringBefore("abc", "d") = "abc" Strings.substringBefore("abc", "") = "" Strings.substringBefore("abc", null) = "abc"- Parameters:
str- the String to get a substring from, may be nullseparator- the String to search for, may be null- Returns:
- the substring before the first occurrence of the separator,
nullif null String input
-
split
public static String[] split(String str, String separatorChars)
Splits the provided text into an array, separators 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
nullinput String returnsnull. AnullseparatorChars splits on whitespace.Strings.split(null, *) = null Strings.split("", *) = [] Strings.split("abc def", null) = ["abc", "def"] Strings.split("abc def", " ") = ["abc", "def"] Strings.split("abc def", " ") = ["abc", "def"] Strings.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]- Parameters:
str- the String to parse, may be nullseparatorChars- the characters used as the delimiters,nullsplits on whitespace- Returns:
- an array of parsed Strings,
nullif null String input
-
repeat
public static String repeat(char ch, int repeat)
Returns padding using the specified delimiter repeated to a given length.
Strings.repeat('e', 0) = "" Strings.repeat('e', 3) = "eee" Strings.repeat('e', -2) = ""- Parameters:
ch- character to repeatrepeat- number of times to repeat char, negative treated as zero- Returns:
- String with repeated character
-
-