Class StringUtil

java.lang.Object
org.nuiton.util.StringUtil

public class StringUtil extends 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
  • Field Details

    • EMPTY_STRING_ARRAY

      public static final String[] EMPTY_STRING_ARRAY
    • timeFactors

      protected static final double[] timeFactors
    • timeUnites

      protected static final String[] timeUnites
    • memoryFactors

      protected static final double[] memoryFactors
    • memoryUnites

      protected static final String[] memoryUnites
  • Constructor Details

    • StringUtil

      protected StringUtil()
      Constructor for the StringUtil object
  • Method Details

    • isEmail

      public static boolean isEmail(String str)
      Know if a string is a valid e-mail.
      Parameters:
      str - a string
      Returns:
      true if str is syntactically a valid e-mail address
      Since:
      2.1
    • escapeCsvValue

      public static String escapeCsvValue(String value, String csvSeparator)
      Add quotes if needed to escape special csv chars (',', '\n', '\t', ',', ';', '"')
      Parameters:
      value - to escape
      csvSeparator - separator used for csv
      Returns:
      escaped if needed value
    • join

      public static String join(Iterable<?> iterable, String separator, boolean trim)
      Used to concat an iterable of Object separated by separator using the toString() method of each object. You can specify if the string must be trimmed or not.
      Parameters:
      iterable - Iterable with objects to treate
      separator - to used
      trim - 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> String join(Iterable<O> iterable, StringUtil.ToString<O> ts, String separator, boolean trim) throws NullPointerException
      Used to concat an iterable of object <O> separated by separator. This method need a ts contract to call on each object. The ToString can be null to use directly the toString() method on the object. The trim boolean is used to specify if each string object has to be trimmed. The null elements in the list will be ignored.
      Type Parameters:
      O - type of object in the list
      Parameters:
      iterable - Iterable with objects to treate
      ts - used to specify how the object is converted in String
      separator - to used between each object string
      trim - 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:
      NullPointerException - if iterable is null.
    • substring

      public static String substring(String s, int begin)
      substring from begin to end of s example: substring("tatetitotu", -4) → totu
      Parameters:
      s - the string to substring
      begin - if begin < 0 then begin start at end of string - begin
      Returns:
      the result of substring
    • substring

      public static String substring(String s, int begin, int end)
      substring from begin to end of s example: substring("tatetitotu", -4, -2) → to
      Parameters:
      s - the string to substring
      begin - if begin < 0 then begin start at end of string - begin
      end - if end < 0 then end start at end of string - end
      Returns:
      the result of substring
    • split

      public static String[] split(String args, 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 split
      separator - separator use to split string
      Returns:
      array of string
    • split

      public static String[] split(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 String[] split(Character[] openingChars, Character[] closingChars, String args, String separator)
      Split string use 'separator' as separator. If String contains "' and openingChar closingChars 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:
      openingChars - list of opening caracteres
      closingChars - list of closing caracteres
      args - string to split
      separator - separator use to split string
      Returns:
      array of string
    • toBoolean

      public static boolean toBoolean(String s)
    • toByte

      public static byte toByte(String s)
    • toDouble

      public static double toDouble(String s)
    • toFloat

      public static float toFloat(String s)
    • toLong

      public static long toLong(String s)
    • toShort

      public static short toShort(String s)
    • toInt

      public static int toInt(String s)
    • toChar

      public static char toChar(String s)
    • toArrayBoolean

      public static boolean[] toArrayBoolean(String... s)
    • toArrayByte

      public static byte[] toArrayByte(String... s)
    • toArrayDouble

      public static double[] toArrayDouble(String... s)
    • toArrayFloat

      public static float[] toArrayFloat(String... s)
    • toArrayLong

      public static long[] toArrayLong(String... s)
    • toArrayShort

      public static short[] toArrayShort(String... s)
    • toArrayInt

      public static int[] toArrayInt(String... s)
    • toArrayChar

      public static char[] toArrayChar(String... s)
    • asHex

      public static 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

      public static Color toColor(String s) throws StringUtilException
      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:
      IllegalArgumentException - FIXME
      StringUtilException - if any problem while conversion
    • toDate

      public static Date toDate(String s) throws ParseException
      Throws:
      ParseException
    • 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:
    • convertTime

      public static String convertTime(long value, long value2)
      Converts an time period into a human readable format.
      Parameters:
      value - the begin time
      value2 - the end time
      Returns:
      the time representation of the given value
      See Also:
    • 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

      public static String convert(long value, double[] factors, String[] unites)
      Note: this method use the current locale (the Locale.getDefault()) in the method MessageFormat(String).
      Parameters:
      value - value to convert
      factors - facotrs used form conversion
      unites - libelle of unites to use
      Returns:
      the converted representation of the given value
    • checkEnclosure

      public static boolean checkEnclosure(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 verifier
      opener - le caractère ouvrant
      closer - le caractère fermant
      Returns:
      true is la chaine est valide
    • convertToConstantName

      public static String convertToConstantName(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 String encodeMD5(String toEncode)
      Convert a String to MD5.
      Parameters:
      toEncode - string concerned
      Returns:
      md5 corresponding
      Throws:
      IllegalStateException - if could not found algorithm MD5
    • 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
    • getFileSeparatorRegex

      public static String getFileSeparatorRegex()
      Returns:
      the file separator escaped for a regex regarding the os used.