Package org.nuiton.util
Class StringUtil
java.lang.Object
org.nuiton.util.StringUtil
public class StringUtil
extends java.lang.Object
Classe contenant un ensemle de methode static utiles pour la manipulation des
chaine de caractere mais qui ne sont pas defini dans la classe String de
Java.
Created: 21 octobre 2003
- Author:
- Benjamin Poussin - poussin@codelutin.com, Tony Chemit - chemit@codelutin.com
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classStringUtil.ToCSV<O>Used to build csv file usingjoin(Iterable, ToString, String, boolean)method.static interfaceStringUtil.ToString<O>Contract to use injoin(Iterable, ToString, String, boolean)method. -
Field Summary
Fields Modifier and Type Field Description static java.lang.String[]EMPTY_STRING_ARRAYprotected static double[]memoryFactorsprotected static java.lang.String[]memoryUnitesprotected static double[]timeFactorsprotected static java.lang.String[]timeUnites -
Constructor Summary
Constructors Modifier Constructor Description protectedStringUtil()Constructor for the StringUtil object -
Method Summary
Modifier and Type Method Description static java.lang.StringasHex(byte[] hash)Turns array of bytes into string representing each byte as unsigned hex number.static booleancheckEnclosure(java.lang.String txt, char opener, char closer)Vérifie q'une chaine de caractère est valid pour les bloc openner closer, ie.static java.lang.Stringconvert(long value, double[] factors, java.lang.String[] unites)Note: this method use the current locale (theLocale.getDefault()) in the methodMessageFormat(String).static java.lang.StringconvertMemory(long value)Converts an memory measure into a human readable format.static java.lang.StringconvertTime(long value)Converts an time delay into a human readable format.static java.lang.StringconvertTime(long value, long value2)Converts an time period into a human readable format.static java.lang.StringconvertToConstantName(java.lang.String name)Convertir un nom en une constante Java Les seuls caractères autorisés sont les alpha numériques, ains que l'underscore. tous les autres caractères seront ignorés.static java.lang.StringencodeMD5(java.lang.String toEncode)Convert a String to MD5.static java.lang.StringencodeSHA1(java.lang.String toEncode)Convert a String to SHA1.static java.lang.StringescapeCsvValue(java.lang.String value, java.lang.String csvSeparator)Add quotes if needed to escape special csv chars (',', '\n', '\t', ',', ';', '"')static java.lang.StringgetFileSeparatorRegex()static booleanisEmail(java.lang.String str)Know if a string is a valid e-mail.static java.lang.Stringjoin(java.lang.Iterable<?> iterable, java.lang.String separator, boolean trim)Used to concat aniterableof Object separated byseparatorusing the toString() method of each object.static <O> java.lang.Stringjoin(java.lang.Iterable<O> iterable, StringUtil.ToString<O> ts, java.lang.String separator, boolean trim)Used to concat aniterableof object<O>separated byseparator.static java.lang.String[]split(java.lang.Character[] openingChars, java.lang.Character[] closingChars, java.lang.String args, java.lang.String separator)Split string use 'separator' as separator.static java.lang.String[]split(java.lang.String stringList)Use to split string array representation in array according with ',' as default separator.static java.lang.String[]split(java.lang.String args, java.lang.String separator)Split string use 'separator' as separator.static java.lang.Stringsubstring(java.lang.String s, int begin)substring from begin to end of s example: substring("tatetitotu", -4) → totustatic java.lang.Stringsubstring(java.lang.String s, int begin, int end)substring from begin to end of s example: substring("tatetitotu", -4, -2) → tostatic boolean[]toArrayBoolean(java.lang.String... s)static byte[]toArrayByte(java.lang.String... s)static char[]toArrayChar(java.lang.String... s)static double[]toArrayDouble(java.lang.String... s)static float[]toArrayFloat(java.lang.String... s)static int[]toArrayInt(java.lang.String... s)static long[]toArrayLong(java.lang.String... s)static short[]toArrayShort(java.lang.String... s)static booleantoBoolean(java.lang.String s)static bytetoByte(java.lang.String s)static chartoChar(java.lang.String s)static java.awt.ColortoColor(java.lang.String s)Essai de convertir une chaine de caractere en une couleur si possible si ce n'est pas possible retourne null.static java.util.DatetoDate(java.lang.String s)static doubletoDouble(java.lang.String s)static floattoFloat(java.lang.String s)static inttoInt(java.lang.String s)static longtoLong(java.lang.String s)static shorttoShort(java.lang.String s)
-
Field Details
-
EMPTY_STRING_ARRAY
public static final java.lang.String[] EMPTY_STRING_ARRAY -
timeFactors
protected static final double[] timeFactors -
timeUnites
protected static final java.lang.String[] timeUnites -
memoryFactors
protected static final double[] memoryFactors -
memoryUnites
protected static final java.lang.String[] memoryUnites
-
-
Constructor Details
-
StringUtil
protected StringUtil()Constructor for the StringUtil object
-
-
Method Details
-
isEmail
public static boolean isEmail(java.lang.String str)Know if a string is a valid e-mail.- Parameters:
str- a string- Returns:
- true if
stris syntactically a valid e-mail address - Since:
- 2.1
-
escapeCsvValue
public static java.lang.String escapeCsvValue(java.lang.String value, java.lang.String csvSeparator)Add quotes if needed to escape special csv chars (',', '\n', '\t', ',', ';', '"')- Parameters:
value- to escapecsvSeparator- separator used for csv- Returns:
- escaped if needed value
-
join
public static java.lang.String join(java.lang.Iterable<?> iterable, java.lang.String separator, boolean trim)Used to concat aniterableof Object separated byseparatorusing the toString() method of each object. You can specify if the string must be trimmed or not.- Parameters:
iterable- Iterable with objects to treateseparator- to usedtrim- if each string must be trim- Returns:
- the String chain of all elements separated by separator, never return null, will return an empty String for an empty list.
-
join
public static <O> java.lang.String join(java.lang.Iterable<O> iterable, StringUtil.ToString<O> ts, java.lang.String separator, boolean trim) throws java.lang.NullPointerExceptionUsed to concat aniterableof object<O>separated byseparator. This method need atscontract to call on each object. The ToString can be null to use directly the toString() method on the object. Thetrimboolean is used to specify if each string object has to be trimmed. The null elements in thelistwill be ignored.- Type Parameters:
O- type of object in the list- Parameters:
iterable- Iterable with objects to treatets- used to specify how the object is converted in Stringseparator- to used between each object stringtrim- if trim() method need to by apply on each object string- Returns:
- the String chain of all elements separated by separator, never return null, will return an empty String for an empty list.
- Throws:
java.lang.NullPointerException- if iterable isnull.
-
substring
public static java.lang.String substring(java.lang.String s, int begin)substring from begin to end of s example: substring("tatetitotu", -4) → totu- Parameters:
s- the string to substringbegin- if begin < 0 then begin start at end of string - begin- Returns:
- the result of substring
-
substring
public static java.lang.String substring(java.lang.String s, int begin, int end)substring from begin to end of s example: substring("tatetitotu", -4, -2) → to- Parameters:
s- the string to substringbegin- if begin < 0 then begin start at end of string - beginend- if end < 0 then end start at end of string - end- Returns:
- the result of substring
-
split
public static java.lang.String[] split(java.lang.String args, java.lang.String separator)Split string use 'separator' as separator. If String contains "'()[]{} this method count the number of open char end close char to split correctly argument WARNING: cette method ne fonctionne pas si le contenu contient des carateres utilisé pour le parsing et présent une seule fois. Par exemple: "l'idenfiant" contient ' qui empeche totalement le parsing de fonctionner.- Parameters:
args- string to splitseparator- separator use to split string- Returns:
- array of string
-
split
public static java.lang.String[] split(java.lang.String stringList)Use to split string array representation in array according with ',' as default separator. WARNING: cette method ne fonctionne pas si le contenu contient des carateres utilisé pour le parsing et présent une seule fois. Par exemple: "l'idenfiant" contient ' qui empeche totalement le parsing de fonctionner.- Parameters:
stringList- string that represent array- Returns:
- array with length > 0 if listAsString ≠ null or null
-
split
public static java.lang.String[] split(java.lang.Character[] openingChars, java.lang.Character[] closingChars, java.lang.String args, java.lang.String separator)Split string use 'separator' as separator. If String contains "' andopeningCharclosingCharsthis method count the number of open char end close char to split correctly argument WARNING: cette method ne fonctionne pas si le contenu contient des carateres utilisé pour le parsing et présent une seule fois. Par exemple: "l'idenfiant" contient ' qui empeche totalement le parsing de fonctionner.- Parameters:
openingChars- list of opening caracteresclosingChars- list of closing caracteresargs- string to splitseparator- separator use to split string- Returns:
- array of string
-
toBoolean
public static boolean toBoolean(java.lang.String s) -
toByte
public static byte toByte(java.lang.String s) -
toDouble
public static double toDouble(java.lang.String s) -
toFloat
public static float toFloat(java.lang.String s) -
toLong
public static long toLong(java.lang.String s) -
toShort
public static short toShort(java.lang.String s) -
toInt
public static int toInt(java.lang.String s) -
toChar
public static char toChar(java.lang.String s) -
toArrayBoolean
public static boolean[] toArrayBoolean(java.lang.String... s) -
toArrayByte
public static byte[] toArrayByte(java.lang.String... s) -
toArrayDouble
public static double[] toArrayDouble(java.lang.String... s) -
toArrayFloat
public static float[] toArrayFloat(java.lang.String... s) -
toArrayLong
public static long[] toArrayLong(java.lang.String... s) -
toArrayShort
public static short[] toArrayShort(java.lang.String... s) -
toArrayInt
public static int[] toArrayInt(java.lang.String... s) -
toArrayChar
public static char[] toArrayChar(java.lang.String... s) -
asHex
public static java.lang.String asHex(byte[] hash)Turns array of bytes into string representing each byte as unsigned hex number.- Parameters:
hash- Array of bytes to convert to hex-string- Returns:
- Generated hex string
-
toColor
Essai de convertir une chaine de caractere en une couleur si possible si ce n'est pas possible retourne null.- Parameters:
s- la couleur sous la forme de string, par exemple "red", "yellow" ou bien en RGB "#FFAA99", et avec un canal alpha "#FFAA3366"- Returns:
- la couleur demandé si possible sinon null
- Throws:
java.lang.IllegalArgumentException- FIXMEStringUtilException- if any problem while conversion
-
toDate
public static java.util.Date toDate(java.lang.String s) throws java.text.ParseException- Throws:
java.text.ParseException
-
convertTime
public static java.lang.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 java.lang.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 java.lang.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 java.lang.String convert(long value, double[] factors, java.lang.String[] unites)Note: this method use the current locale (theLocale.getDefault()) in the methodMessageFormat(String).- Parameters:
value- value to convertfactors- facotrs used form conversionunites- libelle of unites to use- Returns:
- the converted representation of the given value
-
checkEnclosure
public static boolean checkEnclosure(java.lang.String txt, char opener, char closer)Vérifie q'une chaine de caractère est valid pour les bloc openner closer, ie. que les blocs définit par les deux caractères s'entrechevauchent pas. Exemple avec '(' ')' : (a(b)) est valide, par contre ((aaa))) n'est pas valide- Parameters:
txt- txte a verifieropener- le caractère ouvrantcloser- le caractère fermant- Returns:
trueis la chaine est valide
-
convertToConstantName
public static java.lang.String convertToConstantName(java.lang.String name)Convertir un nom en une constante Java Les 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
-
encodeMD5
public static java.lang.String encodeMD5(java.lang.String toEncode)Convert a String to MD5.- Parameters:
toEncode- string concerned- Returns:
- md5 corresponding
- Throws:
java.lang.IllegalStateException- if could not found algorithm MD5
-
encodeSHA1
public static java.lang.String encodeSHA1(java.lang.String toEncode)Convert a String to SHA1.- Parameters:
toEncode- string to encode- Returns:
- sha1 corresponding
- Throws:
java.lang.IllegalStateException- if could not found algorithm SHA1
-
getFileSeparatorRegex
public static java.lang.String getFileSeparatorRegex()- Returns:
- the file separator escaped for a regex regarding the os used.
-