8

I'm looking for a way to convert a string that contains a character escape sequence into the represented character.

So, for instance, I want to parse the string \" (which has two characters, a backslash and a double-quote) into the char ". So, an array of chars into one char.

So something that might do something like this and vice versa:

package test;
public class Test {
    private static char parseChar(String string) {
        char c = 0;
        if ("\\n".equals(string)) {
            c = '\n';
        }else if ("\\t".equals(string)) {
            c = '\t';
        }else if ("\\r".equals(string)) {
            c = '\r';
        }else if ("\\f".equals(string)) {
            c = '\f';
        }else if ("\\b".equals(string)) {
            c = '\b';
        }else if ("\\\'".equals(string)) {
            c = '\'';
        }else if ("\\\"".equals(string)) {
            c = '\"';
        }else if ("\\\\".equals(string)) {
            c = '\\';
        }
        return c;
    }
    public static void main(String[] args) {
        for (String arg : args) {
            System.out.println(arg + " : " + (int)parseChar(arg) + " : " + parseChar(arg) + ";");
        }
    }
}

I could not believe there is nothing in java.lang or other that can provide me with good (maybe native) code for this because I feel the above code might be incomplete and not parse every problematic (escapable?) character, because well I'm a noob. I want a tool that can do the same thing as the String constructor :

String st = "\"";
char ch = st.charAt(0);

ch output : ";

Thank you for reading this, I am sorry if not clear I will check regularly and correct if asked.

PS:

When I run the above code:

java -classpath ~/workspace/MacroRecorder/bin/ test.Test \\n \\t \\f \\r \\b \\\' \\\" \\\\;

...it outputs

\n : 10 : 
;
\t : 9 :    ;
\f : 12 : 
          ;
;r : 13 : 
\b : 8 :;
\' : 39 : ';
\" : 34 : ";
\\ : 92 : \;

But in Eclipse, the output is completely different with the same parameters especially the " is very messy.

sinekonata
  • 364
  • 3
  • 11

1 Answers1

13

Apache Commons to the rescue with StringEscapeUtils, you want the unescapeJava method I think: http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringEscapeUtils.html#unescapeJava(java.lang.String)

Dan Hardiker
  • 3,013
  • 16
  • 19
  • i think this does the exact opposite of what I want but it's a good start as I wanted to have a 2 way util. – sinekonata Jan 05 '12 at 00:31
  • Try the escapeJava method on the same class! It does it both ways, and there are other methods (such as Javascript, HTML etc) in case you want different types of escaping. – Dan Hardiker Jan 05 '12 at 00:37
  • Oh sorry I didn't see there were both escape and unescape thanks a lot I looked for hours for such a class – sinekonata Jan 05 '12 at 00:38
  • public static java.lang.String unescapeJava(java.lang.String str) Unescapes any Java literals found in the String. For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'. Parameters: str - the String to unescape, may be null Returns: a new unescaped String, null if null string input – Dan Hardiker Jan 05 '12 at 00:41
  • So thanks I will use your answer in all those unanswered posts if you don't mind. have a nice day – sinekonata Jan 05 '12 at 00:43