java boilerplate

Thursday, December 01, 2005

String Literals

Entering html, xml, or especially regular expressions in java tends to be a difficult exercise due to the need to enter many escapes.

We propose a string literal operator: strings contained inside matching pairs of 3 quotes are read directly, without parsing single quotes, backslashes, or newlines. Only the \u escape is parsed (as that occurs on a different level).

old code:
     return "Usage:   \nmyapp \"filename\" -options\n";


new code:
     return """Usage:
myapp "filename" -options
""";

3 Comments:

  • your example does show that this is not so handy as a replacement to the \n and \t.

    By Blogger Cristiano Betta, at 11:49 PM  

  • But it is - usually when using a lot of \t and \n, you are formatting either XML or HTML, or some sort of Usage help for a command line tool. In all such cases it's much easier to just type exactly what you intend to put into the XML/HTML/terminal, without the hassle of using multiple concatenated strings or a StringBuilder.

    I admit the example is just as simple in the current non-string literal form, but once you have 10 lines of Usage info, the non-string literal form is an unreadable mess.

    By Blogger Reinier Zwitserloot, at 6:18 PM  

  • But it is - usually when using a lot of \t and \n, you are formatting either XML or HTML, or some sort of Usage help for a command line tool. In all such cases it's much easier to just type exactly what you intend to put into the XML/HTML/terminal, without the hassle of using multiple concatenated strings or a StringBuilder.

    I admit the example is just as simple in the current non-string literal form, but once you have 10 lines of Usage info, the non-string literal form is an unreadable mess.

    By Blogger Reinier Zwitserloot, at 6:19 PM  

Post a Comment

<< Home