Skip to main content

String literals and escapes

2 replies [Last post]
archangel
Offline
Joined: 2003-07-01

I would like to see the C# feature of a prefix character to String literals which tells Java not to expect escape sequences in the String.

This would make file names simpler:

<br />
  String filename = "c:\\myproject\\mysubproject\\test.txt";<br />
  // ...would become...<br />
  String filename = @"c:\myproject\mysubproject\test.txt";<br />

As would regular expressions:

<br />
  String urlRegEx = "(http)(://)(.*?)/(.*?)\\?(.*)"<br />
  // ...would become...<br />
  String urlRegEx = @"(http)(://)(.*?)/(.*?)\?(.*)"<br />

Particularly when filenames get long or regular expressions get more complicated it would make them much easier to read.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dtrehas
Offline
Joined: 2004-03-15

+1 to this feature!. This is a way to escape from the escape hell!

dgreenbean
Offline
Joined: 2003-12-08

+1

Is there any reason not to do this? When Java introduced regular expression matching into the standard API with 1.4, it was a huge help. This seems like the next logical step. My only fear would be that once this is added, other shortcuts like this cound be introduced that are not as necessary. IMO, shortcuts like the following are not necessary at all:
String str = << blah
blah
END

I would much rather see:
String str = "
blah
blah";

Honestly, I have no problem with:
String str = "\nblah\nblah";

Definitely +1 for the regular expression and file name escaping though.

-David