Solved

Print backward slash

Posted on 2006-10-25
8
2,208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 58
Which non-HTML GUI front end to use with Java? 3 52
CSV file parsing thru Java 13 39
Java program running SQL query 5 48
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

756 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