How to escape curly brackets (braces) in JavaFX? I know how, but this is dull…

Well I need to write something like:

System.out.println("NewClass{} is just a way to call NewClass constructor");

of course it fails to compile because it expects some code in the curly brackets, then it would execute it and replace curly bracketed code with execution result. How to avoid it?
I found one way to escape it:

System.out.println("NewClass\u007B\u007D is just a way to call NewClass constructor");

However it looks pretty dull (as if I tried to embed some Unicode charter which does not fit current encoding or hard to find on keyboard🙂 )
May be there is a better way to escape? (Octal and hex escapes are also considered dull🙂 )
\{ \} is not working, alas…

3 Responses to How to escape curly brackets (braces) in JavaFX? I know how, but this is dull…

  1. Jim Weaver says:

    You only need to escape the { not the }

    Use the backslash \ to escape. You can also escape embedded double quotes with a \

    Thanks,
    Jim Weaver
    Helping You Become a “JavaFXpert” Weblog:
    http://javafxpert.com

  2. Thanky, Jim!
    Shame on me, I tried to escape them both, and I got something like
    Error in file:/home/alex/NetBeansProjects/L2/src/l2/Main.fx:27: “file:/home/alex/NetBeansProjects/L2/src/l2/Main.fx”: Lexical error at line 27, column 31. Encountered: “}” (125), after : “\\”
    The message is pretty clear I should understand that only \} is prohibited.

    Thanks a lot!
    As a guru you may be interesting in the following construct:
    System.out.println("{ "{ "{100}" }" }");
    which produces 100.
    The idea of this: enclose into string concatenation another construction that is concatenation it self…
    System.out.println("1 level\{{ "2 level\{{ "3 level has value = {100}" }}" }}");
    which produces:
    1 level{2 level{3 level has value = 100}}

    Alas the latest NetBeans plugin is not capable to render it with nice highlighting🙂

  3. However ability to escape closing bracket would be very helpful…
    say in Java in “” you should not generally escape ‘ but you could if you like, and vice versa in ” you may choose to escape ” or not.

    so

    case ‘\”‘:
    case ‘\”:
    // handle quotes

    looks much better than

    case ‘”‘:
    case ‘\”:
    // handle quotes

    That is why I would have \} escape sequence…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: