Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Escape Characters?

 
Randy Trover
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Randy
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
For:
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).]
 
Jason Ford
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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\'.
Clear?
Jason
 
Jason Ford
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Dave.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic