Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Print backward slash

Posted on 2006-10-25
8
Medium Priority
?
2,233 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 672 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 664 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 664 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

715 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