GeeCON Prague 2014*
The moose likes Java in General and the fly likes Sanitizing String in Java. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Sanitizing String in Java." Watch "Sanitizing String in Java." New topic
Author

Sanitizing String in Java.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

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?


Pawan Chopra
SCJP - DuMmIeS mInD
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

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

Joined: Jan 10, 2002
Posts: 61315
    
  66

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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sanitizing String in Java.