Wednesday, November 11, 2009

Escape Quotes using Java

public static String escapeQuotes(final String text){

String resultString = null;
String buffer = null;
String [] quoteSplit = null;

try {

if((text == null) || (text.equals("")) || (text.indexOf("'") == -1)) {

resultString = text;
} else {

buffer = "";

quoteSplit = (text + " ").split("'");

if(quoteSplit.length > 1) {

for(int i = 0; i < quoteSplit.length; i++) {

if(i < quoteSplit.length - 1) {

buffer += quoteSplit[i] + "'||''''||'";
} else {

buffer += quoteSplit[i];
}
}
}
resultString = buffer.trim();;
}
} catch (Exception e) {

e.printStackTrace();
}
return resultString;
}

No comments:

Post a Comment