Solved

Print backward slash

Posted on 2006-10-25
8
2,196 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 78
firstswap challenge 20 65
Starting to learn JAVA, 7 59
Why method in Java which is called from Runnable run() doesn't need to be 'static'? 1 22
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now