Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace function 2

Status
Not open for further replies.

welshone

Programmer
Jul 30, 2001
414
GB
hello,
is there a function in java to replace ' with '' .
so :

String Name = Replace(req.getParameter("Name_Input"), "'", "''");

?
 
Try :

Code:
String reqParam = req.getParameter("Name_Input");
String parsedReqParam = reqParam.replace('"','\'');

Ben
 
alright ben,
I'm trying this ,and its not working.?

eg is I put mary's in for the name, I get no records returned ?
 
Got it the wrong way round, try
Code:
String reqParam = req.getParameter("Name_Input");
String parsedReqParam = reqParam.replace('\'','"');

PS - You need to "escape (\)" the "'" (single quote) because the String.replace() method takes two characters as input, which must be enclosed within single quotes. Hence, escaping the middle single quote.
 
alrighr sebj, thanks for the reply.
however, that doesn't seem to work either ?!
any ideas why ? or other methods ?

thank you.
 
I tested sedj's code and it works fine.

"eg is I put mary's in for the name, I get no records returned"

That sounds like your
String reqParam = req.getParameter("Name_Input");
isn't getting anything to begin with. Check to make sure Name_Input case is correct.
 
I am getting results with other search criteria.
eg, if I type mary I get results, if I type mary's I donot get any results, but thats how the data is stored in the table.
I am using sql server 7, do you know if this wouldcause a problem ?
 
So you're pulling a name to search for from the request object, doing a search for the name in a SQL table, and not getting any results back if there's an apostrophe in the name. Does that describe your actual problem?

How about posting the SQL statement you're using?
 
yeah mate, thats the problem I'm having.

my query is :

String queryString = "SELECT name, Postcode, County, Telephone FROM vEST_DETAILS WHERE name LIKE '%"+Est_Name+"%' AND County LIKE '"+Est_County+"%' AND Postcode LIKE '"+Est_Postcode+"%' ORDER BY name ";
 
OK, so what you're trying to do is replace all the single apostrophes with two apostrophes. In your earlier posts, it looked like you were trying to replace an apostrophe with a double quote...

Try this code:

Code:
/* Replace all instances of a String in a String.
 *   @param  s  String to alter.
 *   @param  f  String to look for.
 *   @param  r  String to replace it with, or null to just remove it.
 */  
public String replace( String s, String f, String r )
{
   if (s == null)  return s;
   if (f == null)  return s;
   if (r == null)  r = "";

   int index01 = s.indexOf( f );
   while (index01 != -1)
   {
      s = s.substring(0,index01) + r + s.substring(index01+f.length());
      index01 += r.length();
      index01 = s.indexOf( f, index01 );
   }
   return s;
}

call it like this:

String parsedReqParam = replace(reqParam,"\'","\'\'");

 
that is excellent.
I've now created this code into its own class so can be reused !!
thank you for this !

cheers guys for all your help.
 
You can also use the regular expression functionality new in java 1.4:

parsedReqParam = (reqParam == null ? null : reqParam.replace("'", "''"));
 
pinkpuppy: Could you expand on that a bit? I checked my 1.4 API docs and I still don't see a replace method on the String object that takes two Strings as parameters.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top