JAVA Trying to .replace the escape character '\' with "\\" for an absolute path.?
JAVA Trying to .replace the escape character ‘\’ with “\” for an absolute path.?
But it is not working. Here is my code
Scanner textReader = new Scanner(InputFile);
String testx = textReader.nextLine();
System.out.println(testx);
testx = testx.replace(‘\’, (char) (‘\’ + ‘\’));
System.out.println(testx);
I have also tried adding up the ASCII characters in the char typecast but at some point it just gives me the question mark symbol no matter how high I go. I know there is a way to do this I am just not experienced enough.
I am trying to replace something like C:\SDIDOCS\DMADMIN\@q#01!.TIF with C:\SDIDOCS\DMADMIN\@q#01!.TIF for well over 5000 entries in a text file. I refuse to do this manually. Help?
Best answer:
Use 3 \\’s
Tags: absolute, character, escape, java, Path, Replace, trying
Under Forum

1 Comment for JAVA Trying to .replace the escape character '\' with "\\" for an absolute path.?
1. Jordan | February 19th, 2008 at 1:24 pm
The problem is that you’re using the wrong replace() method – you’re using one that’s for replacing a SINGLE character (like a lone ) with another SINGLE character (exactly unlike a double backslash: \ ). What you want to do is use another replace() method – the one which is built to replace one string with another:
testx = testReader.nextLine( ).replace( “\”, “\\” );
The version you’ve given will cast the arithmetic sum of the ASCII values of ‘\’ and ‘\’ to a char. Since the code for a backslash is 92 (see sources), the sum of two of them is 184 – well outside the basic ASCII character set, so it gets rendered as a question mark.
Leave a Comment for JAVA Trying to .replace the escape character '\' with "\\" for an absolute path.?
Trackback this post | Subscribe to the comments via RSS Feed