It's not a secret anymore!
The moose likes Beginning Java and the fly likes Tracking Escape character Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tracking Escape character" Watch "Tracking Escape character" New topic

Tracking Escape character

C Balajee

Joined: Oct 18, 2000
Posts: 25
I want to track the existence of '\' in my String variable i have written the following code but I'm not successful,
Looking forward to your help , the code is as follows.

If i change my input string to
Then i'm able to track the '\'character.
I know this happens because we are escaping '\'
but in my application this string may come from an user input where i don't want to have any control over what my user is entering.
thanks for taking time to go through my code.
[This message has been edited by Cindy Glass (edited September 25, 2001).]
Stefan Maric

Joined: Sep 12, 2001
Posts: 8
Try using
int nIndex = 0;
String sText = "hell\o123\4";
String.indexOf('\', nIndex);
you can then adjust nIndex and repeat to find next occurrence
etc etc
C Balajee

Joined: Oct 18, 2000
Posts: 25
Its giving me
"invalid escape sequence" error
when i try to run this snippet of code
Is there any other way...
Thanks again.
Bill Hause

Joined: Jan 17, 2001
Posts: 13
As you pointed out, adding the second '\' "pankaj\\1234" fixes the problem because you are escaping the '\'.
Escaping is not necessary for user-entered strings. Therefore, you code should work as-is on strings entered by the user.
Try getting your string from a JOptionPane and your code should work as-is.
String text = JOptionPane.showInputDialog("Enter a String");


---------------------<BR>William D. Hause<BR>
It is sorta covered in the JavaRanch Style Guide.
subject: Tracking Escape character
It's not a secret anymore!