Solved

Print backward slash

Posted on 2006-10-25
8
2,192 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
count8 challlenge 13 87
JAVA part two 5 41
Java - Why doesn't this JFrame work 3 18
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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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

17 Experts available now in Live!

Get 1:1 Help Now