Class StringEscape


  • public class StringEscape
    extends Object

    This class can resolve escaped characters in a string and it can replace special characters with escaped characters. It supports the following escapes:

    • \n -> new line
    • \r -> carriage return
    • \t -> tab
    • \c (where c is any other character including \ and ") -> c
    • Constructor Detail

      • StringEscape

        public StringEscape()
    • Method Detail

      • resolveEscapes

        public static String resolveEscapes​(String s)
                                     throws ParseException
        Replaces escaped characters in the specified string. This method throws an exception if it finds a lone \.
        Parameters:
        s - a string
        Returns:
        the string without escapes
        Throws:
        ParseException - if a lone \ is found
      • resolveEscapes

        public static String resolveEscapes​(String s,
                                            char[] illegals)
                                     throws ParseException
        Replaces escaped characters in the specified string. This method throws an exception if it finds a lone \ or if one of the specified illegal characters is not escaped.
        Parameters:
        s - a string
        illegals - characters that must be escaped
        Returns:
        the string without escapes
        Throws:
        ParseException - if a lone \ is found or if one of the illegal characters is not escaped
      • escape

        public static String escape​(String s)
        Replaces special characters with an escape sequence. It escapes the characters listed at the top of this page.
        Parameters:
        s - a string
        Returns:
        the escaped string
      • escape

        public static String escape​(String s,
                                    char[] illegals)
        Replaces special characters with an escape sequence. It escapes the characters listed at the top of this page as well as the specified illegal characters (which are considered illegal if they are not escaped).
        Parameters:
        s - a string
        illegals - characters that must be escaped
        Returns:
        the escaped string