Solved

Print backward slash

Posted on 2006-10-25
8
2,206 Views
Last Modified: 2008-03-03
String l_strData = "1\2";
l_strData = l_strData.replace( "\\","\\\\");

System.out.println(l_strData);

Above code printed 1<face>. How to make it print 1\2 ?

I tried replaceAll method, it does not help either.
0
Comment
Question by:weiyee
8 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 168 total points
ID: 17806510
System.out.println("1\\2");
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17806525
Or
String l_strData = "1\2";
l_strData = l_strData.replace("\2","\\2");
0
 

Author Comment

by:weiyee
ID: 17806670
Sorry ! I should make it clear. I want to print the backward slash, regardless in what string. It can be anything after \.

1\2 is just an example.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 14

Expert Comment

by:hoomanv
ID: 17806690
> I want to print the backward slash
Where ? Can you explain more ?
0
 
LVL 2

Assisted Solution

by:avsrivastava
avsrivastava earned 166 total points
ID: 17807101
If the strings that you want to print are not quoted strings(that they do not appear directly in code);
try printing them as it is. They will get printed correctly.

However, if the string is a quoted string and it contains a \ed character, you can not do anything because the character '\' is not present in the string.

Try this example:

class Slash
{
        public static void main(String[] args) throws IOException
        {
                System.out.println(args[0]+args[1]);
                String str = "asd\r";
                System.out.println(str.indexOf("\\"));
               BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                System.out.println(br.readLine());
       }
}

then run it as
/> java Slash 'arc\r' 'arc\\asd'

Input: as\\\ds\\ad\asd

Output:

arc\rarc\\asd
-1
as\\\ds\\ad\asd

the -1 shows the quoted string does ot contain '\' character
the other strings printed correctly without doing anything
0
 
LVL 5

Assisted Solution

by:gkishoreji
gkishoreji earned 166 total points
ID: 17808999
String l_strData = "1\2";
\2 will be considered as a single character so
l_strData.replace( "\\","\\\\"); wont work because there is no '\' present in l_strData

if u want it to work properly, u must construct the string l_strData properly (ie. append '\\' whereever u want to print '\' ) rather than doing a replace at the end..
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question