Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sanitizing String in Java.

 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a program which consist of following code:



This is a simple code to read input from the console. Now If I type this input "I am\Pawan" which is actually illegal String in Java because of single "\". This gets sanitized and the value of str that we see while debugging is "I am\\Pawan". I want to see where this correction is happening. Can any one guide me to some tutorials on validating string values and making correction where ever required and also how and where java is doing this?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pawan chopra wrote:Now If I type this input "I am\Pawan" which is actually illegal String in Java because of single "\".

Incorrect. It is a perfectly valid string. What it is not, is a perfectly valid string literal.

Strings typed as input are not string literals -- they're just strings -- so the format doesn't need to conform to any rules for literals.

Nothing is "sanitizing the string" -- it's fine just as it is. It's literal representation is what contains the escape characters, not the string itself.
 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Strings typed as input are not string literals -- they're just strings -- so the format doesn't need to conform to any rules for literals.


Thanks Bear. It looks like a great point but can you please explain bit more. I didn't understand that "Strings typed as input are not string literals -- they're just strings"
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are strings -- just a sequence of characters. And there are no rules as to what they can contain.

A "string literal" is a way of expressing the value of a string in code. The escaping mechanism is necessary so that characters that could not otherwise be expressed in the value can be expressed. For example \n to represent new-line. This escaping has nothing to do with strings in memory, it's just a short-hand for expressing a string value in the code.

So to say that a string cannot contain the value "\P" is wrong. Of course it can. However, in order to express this in a literal, you must type "\\P". But the in-memory value of the string is still "\P".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic