Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to read the string properly

 
Karthik Sanyasi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When I'm trying to replace a particular character "\" in the String to "/". It gives me a different output as "02 ?0" for my program. After browsing I understood that this character "\" while reading, java reads it like an escaped character.

So can anyone help me in this that gives me the resulting output as "02/13/2010"

public class StringToDate {
public static void main(String[] args) {
String s = "02\13\2010";
String str = s.replace("\\","/");
System.out.println(str);
}

}

Output is: 02 ?0

The Output that I'm expecting is : 02/13/2010
 
Paul Clapham
Sheriff
Pie
Posts: 20764
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That string doesn't contain any backslash characters. Print it out to see what it actually looks like. You should have this instead:
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Karthik Sanyasi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tnx Paul for the quick reply.
I see what you saying.
So basically when I'm reading a string of some text file, and if that text has 02\13\2010 then the string reads it like 02\\13\\2010. If that's your point then what's the way that I can replace that character to "/" and get my resulting output as 02/13/2010.

I tried it like as you said but still I get the output as "02\13\2010" and not "02/13/2010"




Output s1: 02 ?0
Output s2: 02\13\2010
Output s3: 02 ?0
Output s4: 02\13\2010



 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the code button; since you are new I have edited your post and you can see how much better it looks.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthik Sanyasi wrote:So basically when I'm reading a string of some text file, and if that text has 02\13\2010 then the string reads it like 02\\13\\2010. If that's your point then what's the way that I can replace that character to "/" and get my resulting output as 02/13/2010.
No when you read 02\13\2010 from an input source the String will contain 02\13\2010. However when you use a String literal in your code then you need to escape the backslashes. So that becomes:
String s = "02\\13\\2010";
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic