This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Escape character  RSS feed

 
Rajashekar Subramany
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String x = "\\[";
String y = "\\]";
System.out.println(x + "myTextHere" + y);


Output
----------

\[myTextHere\]


The problem here is that i want my output to be:

Output (expected)
----------
[myTextHere]


Raj.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using the \\ you are escaping the \, so it prints a single \.

Do you actually need escape sequences to print [ or ] when you are not using a regular expression?
Try simply printing "[" and "]"
I tried and got this:-
Output
------

[My Text]
 
Rajashekar Subramany
Greenhorn
Posts: 17
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Works just fine. But i thought was an escape character and many articles on the net advice using instead.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure, but I think you have to escape [] when using regular expressions, but nor in ordinary Strings.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!