Class Strings

java.lang.Object
org.openxava.util.Strings

public class Strings extends Object
Utilities to work with String.

Author:
Javier Paniza
  • Constructor Details

    • Strings

      public Strings()
  • Method Details

    • concat

      public static String concat(String separator, String... strings)
      Concatenates the list of strings using the separator.

      For example, concat(" - ", "Juan", "Perico", "Andrés") returns "Juan - Perico - Andrés".

      Parameters:
      separator - The character used as separator.
      strings - Strings to be concatenated. Can be null.
      Returns:
      Not null, including the case strings == null.
      Since:
      6.0
    • isNumeric

      public static boolean isNumeric(CharSequence string)
      The space, comma, dot, + and - are considered as numeric.
      Since:
      5.1.1
    • changeSeparatorsBySpaces

      public static String changeSeparatorsBySpaces(String string)
      Changes \n, \r and \t by space.

      Parameters:
      string - Not null
    • toCharSet

      public static String toCharSet(String original, String charSet) throws UnsupportedEncodingException
      Translate to the charset specified.

      Parameters:
      original - Original string
      charSet - Charset to traslate to, for example, UTF-8, or ISO-8859-1
      Returns:
      The string translated
      Throws:
      UnsupportedEncodingException - If charset is not supported
    • spaces

      public static String spaces(int count)
      Creates a string with the specified blank spaces.

      Parameters:
      count - Quantity of spaces
      Returns:
      Not null.
    • fix

      public static String fix(String string, int length, Align align)
      Fix the length of the string filling with spaces if needed.

      Returns the sent string but with the specified length.
      It fills or cuts as it needs.

      Parameters:
      string - Can be null, in which case empty string is assumed
      length - Character count of result string
      align - Not null
      Returns:
      Not null
    • fix

      public static String fix(String string, int length, Align align, char fillCharacter)
      Fix the length of the string filling with the specified character if needed.

      Returns the sent string but with the specified length.
      It fills or cuts as it needs.

      Parameters:
      string - Can be null, in which case empty string is assumed
      length - Character count of result string
      align - Not null
      fillCharacter - Character used to fill
      Returns:
      Not null
    • repeat

      public static String repeat(int count, String string)
      Creates a string from repeating another string.

      Parameters:
      count - Times to repeat
      string - String to repeat
      Returns:
      Not null
    • toArray

      public static final String[] toArray(String list)
      Converts a list of comma separated elements in a string array.

      For example, the list Angel, Manolo, Antonia is converted to a array of 3 elements with this 3 names without comman nor spaces.

      Parameters:
      list - String with the list. If null return a empty string
      Returns:
      Not null, including the case list == null.
    • toArray

      public static final String[] toArray(String list, String separator)
      Converts a list of elements separated by a arbitrary character in a string array.

      For example, the list Angel : Manolo : Antonia is converted to a array of 3 elements with this 3 names without colon (for example) nor spaces.

      Parameters:
      list - String with the list. If null return a empty string
      separator - The character used as separator.
      Returns:
      Not null, including the case list == null.
    • toCollection

      public static final Collection<String> toCollection(String list)
      Converts a list of comma separated elements in a string collection.

      For example, the list Angel, Manolo, Antonia is converted to a collection of 3 elements with this 3 names without comman nor spaces.

      Parameters:
      list - String with the list. If null return a empty string
      Returns:
      Not null, including the case list == null.
    • toCollection

      public static final Collection<String> toCollection(String list, String separator)
      Converts a list of elements separated by a arbitrary character in a string collection.

      For example, the list Angel : Manolo : Antonia is converted to a collection of 3 elements with this 3 names without colon (for example) nor spaces.

      Parameters:
      list - String with the list. If null return a empty string
      separator - The character used as separator.
      Returns:
      Not null, including the case list == null.
    • toList

      public static final List<String> toList(String list)
      Converts a list of comma separated elements in a string List.

      For example, the list Angel, Manolo, Antonia is converted to a List of 3 elements with this 3 names without comma nor spaces.

      Parameters:
      list - String with the list. If null return a empty string
      Returns:
      Not null, including the case list == null.
      Since:
      4.3
    • toList

      public static final List<String> toList(String list, String separator)
      Converts a list of elements separated by a arbitrary character in a string List.

      For example, the list Angel : Manolo : Antonia is converted to a List of 3 elements with this 3 names without colon (for example) nor spaces.

      Parameters:
      list - String with the list. If null return a empty string
      separator - The character used as separator.
      Returns:
      Not null, including the case list == null.
      Since:
      4.3
    • toSet

      public static final Set<String> toSet(String list)
      Converts a list of comma separated elements in a string set.

      For example, the list Angel, Manolo, Antonia is converted to a set of 3 elements with this 3 names without comman nor spaces.

      Parameters:
      list - String with the list. If null return a empty string
      Returns:
      Not null, including the case list == null.
      Since:
      4.1
    • toSetNullByPass

      public static final Set<String> toSetNullByPass(String list)
      Converts a list of comma separated elements in a string set.

      For example, the list Angel, Manolo, Antonia is converted to a set of 3 elements with this 3 names without comman nor spaces.

      Parameters:
      list - String with the list. If null return a empty string
      Returns:
      Null in the case list == null.
      Since:
      4.1
    • toSet

      public static final Set<String> toSet(String list, String separator)
      Converts a list of elements separated by a arbitrary character in a string set.

      For example, the list Angel : Manolo : Antonia is converted to a set of 3 elements with this 3 names without colon (for example) nor spaces.

      Parameters:
      list - String with the list. If null return a empty string
      separator - The character used as separator.
      Returns:
      Not null, including the case list == null.
      Since:
      4.1
    • toString

      public static final String toString(Collection collection)
      Converts a collection of objects in a string of comma separated elements.

      For example, a collection of 3 elements with 3 names is converted to the string Angel, Manolo, Antonia

      Parameters:
      collection - Collection with the elements. If null return an empty string
      Returns:
      Not null, including the case collection == null.
    • toString

      public static final String toString(Collection collection, String separator)
      Converts a collection of objects in string of elements separated by a arbitrary character .

      For example, a collection of 3 elements with this 3 names is converted to a string of 3 elements with this 3 names and colon (for example).

      Parameters:
      collection - A collection of objects. If null return an empty string
      separator - The character used as separator.
      Returns:
      Not null, including the case collection == null.
    • toString

      public static final String toString(Object[] array)
      Converts an array of objects in a string of comma separated elements.

      For example, an array of 3 elements with 3 names is converted to the string Angel, Manolo, Antonia

      Parameters:
      array - Array with the elements. If null return a empty string
      Returns:
      Not null, including the case array == null.
      Since:
      5.6
    • toString

      public static final String toString(Object[] array, String separator)
      Converts an array of objects in string of elements separated by a arbitrary character .

      For example, an array of 3 elements with this 3 names is converted to a string of 3 elements with this 3 names and colon (for example).

      Parameters:
      array - An array of objects. If null return an empty string
      separator - The character used as separator.
      Returns:
      Not null, including the case array == null.
      Since:
      5.6
    • toString

      public static final String toString(Object object)
      Try to do a decent toString from a regular object.

      Good format for arrays, dates, numbers, etc. taking in account the current locales (from Locales.getCurrent())

      Since:
      5.9
    • toString

      public static final String toString(Locale locale, Object object)
      Try to do a decent toString from a regular object.

      Good format for arrays, dates, numbers, etc. taking in account the specified locale.

      Since:
      7.6.4
    • toObject

      public static final Object toObject(Class type, String string)
      Converts a string a object of the specified type.

      Supports all primitive type plus its wrappers except char and void.
      Support String and BigDecimal too.

      If there is conversion error or is a type not supporte return null or the default value for the type (zero for numeric).

      Parameters:
      type - The type of returned object (can be a primitive type in this case return its wrapper). Not null
      string - String with data to convert. Can be null, in this case return a default value.
    • firstUpper

      public static String firstUpper(String s)
      Returns a string like the sent one but with the first letter in uppercase.

      If null is sent null is returned.

    • firstLower

      public static String firstLower(String s)
      Returns a string like the sent one but with the first letter in lowercase.

      If null is sent null is returned.

    • change

      public static String change(String string, Map toChange)
      In the sent string changes the strings in toChange map for its values.

      Parameters:
      string - Not null
      toChagne - Not null
    • change

      public static String change(String string, String original, String newString)
      Change in string original by newString.

      Parameters:
      string - String in which we make the changes. Can be null
      original - String to search. Not null
      newString - New value to put in place of original. Not null
      Returns:
      The changed string, if the sent string is null a null is returned
    • changeLast

      public static String changeLast(String string, String original, String newString)
      Change in string the last occurrence of original by newString.

      Parameters:
      string - String in which we make the changes. Can be null
      original - String to search. Not null
      newString - New value to put in place of original. Not null
      Returns:
      The changed string, if the sent string is null a null is returned
    • lastToken

      public static String lastToken(String string)
      Returns:
      If string if null or have no tokens returns empty string.
    • lastToken

      public static String lastToken(String string, String delim)
      Returns:
      If string if null or have no tokens returns empty string.
    • noLastToken

      public static String noLastToken(String string)
      All string but without last token.

      A trim is applied to the result.

      Returns:
      If string if null or have no tokens returns empty string.
    • noLastToken

      public static String noLastToken(String string, String delim)
      All string but without last token.

      Includes the last delim.

      Returns:
      If string if null or have no tokens returns empty string.
    • noFirstToken

      public static String noFirstToken(String string, String delim)
      All string but without first token.

      Includes the delim.

      Returns:
      If string if null or have no tokens returns empty string.
    • noFirstTokenWithoutFirstDelim

      public static String noFirstTokenWithoutFirstDelim(String string, String delim)
      All string but without first token.

      Does not include the delim.

      Returns:
      If string if null or have no tokens returns empty string.
    • noLastTokenWithoutLastDelim

      public static String noLastTokenWithoutLastDelim(String string, String delim)
      All string but without last token.

      It does not include the last delim.

      Returns:
      If string if null or have no tokens returns empty string.
    • firstToken

      public static String firstToken(String string, String delim)
      Returns:
      If string if null or have no tokens returns empty string.
    • removeXSS

      public static String removeXSS(String notSafeValue)
      A key function of any application filtering process will be the removal of possible dangerous special characters.
      Returns:
      new safe string
    • removeXSS

      public static Object removeXSS(Object notSafeValue)
      Parameters:
      notSafeValue -
      Returns:
      Safe Object
    • isJavaIdentifier

      public static boolean isJavaIdentifier(String input)
      Since:
      7.4.5
    • javaIdentifierToNaturalLabel

      public static String javaIdentifierToNaturalLabel(String name)
      Convert a string with a Java identifier in label natural for a human.

      If you send "firstName" it returns "First name".
      If you send "CustomerOrder" it returns "Customer order".
      If you send "first_name" it returns "First name" (snake_case support since 7.4.1).

    • naturalLabelToIdentifier

      public static String naturalLabelToIdentifier(String naturalLabel)
      Convert a string with a label natural for a human into a identifier valid to use as URL, file name, internal id, etc.

      If you send "León, España" it returns "LeonEspana".
      The % is changed by the "percent" string.

      Since:
      5.6
    • removeAccents

      public static String removeAccents(String value)
      Change from a vowel with an accent, to vowel with no accent If you send "Camión" it returns "Camion"
      Since:
      v4m6
    • isModelName

      public static boolean isModelName(String string)
      Determines if the string is a valid model name.
      Since:
      4.5
    • removeBlanks

      public static String removeBlanks(String string)
      Returns the argument string without blanks (\n, \r, \t, \f) or whitespace.

      If you send "my group" it returns "myGroup".
      If you send "My Section" it returns "MySection".

      Parameters:
      string - String not null
      Since:
      5.2.1
    • multiline

      public static String multiline(String... strings)
      Returns a String multiline platform independent.

      For example,

      Strings.multiline("OpenXava", "AJAX Java Framework Web", "You only have to write the domain classes") ->
          "OpenXava
           AJAX Java Framework Web
           You only have to write the domain classes"
      Parameters:
      strings - The array of String objects, entries not may be null
      Since:
      5.7
    • unquote

      public static String unquote(String sentence)
      Remove the quotes from a sentence between quotes.

      That is:

      "Hi, I'm Peter" --> Hi, I'm Peter
      
      Parameters:
      sentence - The original sentence, with or without surrounding quotes.
      Returns:
      The sentence without quotes, if the sentence has no quotes returns the original string.
      Since:
      5.8
    • extractVariables

      public static Collection<String> extractVariables(String text)
      Extract variables names inside ${} from a text.

      So, if you have "select ${number}, ${name} from ${customer} you get a collection with the strings "number", "name" and "customer".

      Parameters:
      text - Text to examine, can be null.
      Returns:
      The collection with the variable names, it never will be null.
      Since:
      6.5.1
    • extractVariables

      public static Collection<String> extractVariables(String text, String startDelimiter, String endDelimiter)
      Extract variables names inside the indicate separators from a text.

      So, if you have "select [number], [name] from [customer] and call sending "[" and "]" as delimiter, you get a collection with the strings "number", "name" and "customer".

      Parameters:
      text - Text to examine, can be null.
      endDelimiter - The delimiter that finished a variable, like } or ].
      Returns:
      The collection with the variable names, it never will be null.
      Since:
      6.5.1
    • wrapVariables

      public static String wrapVariables(String listOfVariables)
      Wrap each variable name in the list with ${}, preserving optional suffixes, like ASC/DESC.

      Examples: - "number, name" -> "${number}, ${name}" - "number desc" -> "${number} desc" - "${number} desc" -> "${number} desc" (left intact) - "customer.name, ${code} asc" -> "${customer.name}, ${code} asc"

      Parameters:
      listOfVariables - List of comma-separated variable names, optionally with a suffix like ASC/DESC
      Returns:
      The list with each variable wrapped; never null
      Since:
      7.6