Class StringEscaper


  • public class StringEscaper
    extends Object
    String Escaper.
    Since:
    15.0
    Version:
    17.0 2020-01-04 14:41:35
    Author:
    Stéphane GALLAND
    Maven Group Id:
    org.arakhne.afc.core
    Maven Artifact Id:
    vmutils
    • Field Detail

      • JAVA_SPECIAL_CHARS

        public static final String[][] JAVA_SPECIAL_CHARS
        Java special characters.
      • JSON_SPECIAL_ESCAPED_CHAR

        public static final String JSON_SPECIAL_ESCAPED_CHAR
        Json special character.
        See Also:
        Constant Field Values
      • JAVA_MIN_CHAR

        public static final int JAVA_MIN_CHAR
        Minimal code of the valid characters for Java strings.
        See Also:
        Constant Field Values
      • JAVA_MAX_CHAR

        public static final int JAVA_MAX_CHAR
        Maximal code of the valid characters for Java strings.
        See Also:
        Constant Field Values
    • Constructor Detail

      • StringEscaper

        public StringEscaper()
        Constructor with the default Java escape character.
      • StringEscaper

        public StringEscaper​(CharSequence toEscapeCharacter,
                             CharSequence... escapeCharacters)
        Constructor with the given escape character.
        Parameters:
        toEscapeCharacter - the escape character within the result string.
        escapeCharacters - the escape characters from the source string.
    • Method Detail

      • setSpecialChars

        public void setSpecialChars​(String[][] chars)
        Change the special characters.

        The given table is an array of string pairs. The first element of each pair is the string of characters to replace. The second element of each pair is the replacement string of characters.

        Parameters:
        chars - the translation table.
      • setValidCharRange

        public void setValidCharRange​(int minValidChar,
                                      int maxValidChar)
        Change the range of the valid characters that should not be escaped. Any character outside the given range is automatically escaped.

        If maxValidChar is lower or equal to zero, the invalid characters are not put within the result of the escaping.

        Parameters:
        minValidChar - the code of the minimal valid character.
        maxValidChar - the code of the maximal valid character.
      • escape

        public String escape​(CharSequence text)
        Escape the given text.
        Parameters:
        text - is the text to convert.
        Returns:
        the Java string for the text.
      • formatHex

        @Pure
        public static String formatHex​(int amount,
                                       int digits)
        Format the given int value to hexadecimal.
        Parameters:
        amount - the value to convert.
        digits - the minimal number of digits.
        Returns:
        a string representation of the given value.