aspose file tools
The moose likes Beginning Java and the fly likes Escape Characters? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Escape Characters?" Watch "Escape Characters?" New topic

Escape Characters?

Randy Trover
Ranch Hand

Joined: Feb 26, 2001
Posts: 34
Hello All,
I'm not sure how to use escape characters in java. I'm writing a short java program that reads lines from a textfile and then writes them to a database. The first place this came up was the path to the database "d:\database\filename.gdb" had to be written
"d:\\database\\filename.gdb". Then in the insert SQL I'm seeing examples like this
int i = st.executeUpdate("insert into kwality
values(\"" + arg[0] + "\", + arg[1] + "\")");
could anybody explain this to me.
Thank and Cheers
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
The problems telling the compiler that you want to treat some characters that it normally thinks of as part of java syntax, that in this case you want it treated as a literal. To do that you use the \ character.
int i = st.executeUpdate("insert into kwality values(\"" + arg[0] + "\", + arg[1] + "\")");
This is trying to get the sentence:
"insert into kwality values("arg[0],arg[1]")"
into the executeUpdate(string); syntax.
The arg[] values need to be concatenated outside of the double quotes like:
executeUpdate("string1" + arg[0] + "string2" + arg[1] + "string3");
However the embedded double quotes would end the first string at the wrong places so you need to break it down into:
"insert into kwality values(\"" + arg[0] + "\", \" "+ arg[1] + "\")"
Where the red quotes are part of the syntax and the black quotes are part of what is trying to get written and therefore need escapes.

[This message has been edited by Cindy Glass (edited September 13, 2001).]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Jason Ford
Ranch Hand

Joined: Aug 02, 2001
Posts: 54
The symbol '\' has a special meaning in many programming languages, including Java. It is called the "escape character", and is used to change the meaning of the thing that follows. For example, how do you think you can create a String that looks like '"Java"'? Here, we want the double quotes to be part of the string. Well, you could try
String javaString = "Java";
However, your string will end up looking like 'Java', without the double quotes. That's because Strings are normally surrounded with double quotes to let Java know it's a String. If you want your String to contain the double quotes, you need to "escape" the special meaning of '"', so Java knows you want the double quotes to be part of the string. We do that like this:
String javaString = "\"Java\"";
Now, how do you get the '\' as part of the string? If you don't escape the '\', Java will think that you want the escape character and not the '\' symbol. To get this, we need to escape the escape character, like such:
String javaString = "\\Java\\";
Now, our string will look like '\Java\'.

Jason Ford
Ranch Hand

Joined: Aug 02, 2001
Posts: 54
Good explanation, Cindy. You beat me to it. I'm just now starting to build up the typing speed I had before I made the switch from QWERTY to Dvorak.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

If you use a PreparedStatement rather than a Statement in the database query, it will take care of all of the special character substitution for you.
Saves a lot of hassle.
jQuery in Action, 2nd edition
subject: Escape Characters?