Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Newsletter Adding slash "\" character before quote "'" in a query

Related Tutorials


 
 

Share on Google+Share on Google+

Adding slash "\" character before quote "'" in a query

Advertisement
Adding slash "\" character before quote "'" in a query Adding slash " \ " character before quote " ' " in a query During the inserting the records in the database if user enters the phrases like "What ' s your name?", database gives the error due

Adding slash "\" character before quote "'" in a query 

     

Adding slash "\" character before quote "'" in a query 



During the inserting the records in the database if user enters the phrases like "What's your name?", database gives the error due to the presence of the "'" quote. So it is necessary to add slashes before the quote. For example if the user enters "What's your name?" then we will have to change it to "What\'s your name?" by placing the "\" character before the quote "'". Following program do this:

import java.util.*;

public class quote{

public String addSlashes(String str){
if(str==null) return "";

StringBuffer s = new StringBuffer ((String) str);
for (int i = 0; i < s.length(); i++)
if (s.charAt (i) == '\'')
s.insert (i++, '\\');
return s.toString();

}

public static void main(String args[]) {
quote qt=new quote();

System.out.println(qt.addSlashes("What's your name?"));
}

}


If you run the above code addSlashes function will add slashe ("\") before the quote "'" and the out put will be shown on the console.


You can use the following function in your java application to add the slashes.

public String addSlashes(String str){
if(str==null) return "";

StringBuffer s = new StringBuffer ((String) str);
for (int i = 0; i < s.length(); i++)
if (s.charAt (i) == '\'')
s.insert (i++, '\\');
return s.toString();

}


Next time I will show you how to use this in java applications.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 30, 2005

Related Tutorials

Discuss: Adding slash "\" character before quote "'" in a query  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
mritunjay shall peelam
September 11, 2011
Java and advance java

find approximate good solution
syntax error
July 9, 2012
backslash character

backslash character! like \n for new line
DMCA.com