|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
||||
|
||||
|
Java: addSlashes() ?
I've searched all over for an addSlashes() method...
does anyone know if such a thing exists? i'm thinking of something along the lines of PHP's addSlashes, where it'll put a \ before every ', ideally for use before executing commands on a database... |
|
#2
|
||||
|
||||
|
I think you'll probably have to roll your own.
|
|
#3
|
||||
|
||||
|
heh, guess i'm just used to being lazy...
I had found one package, although all it did was replace the single quotes with slashes... when i get it done, i'll post the results for others |
|
#4
|
||||
|
||||
|
I was playing around with the replace() method of the String class, only to be frustrated by the fact that it replaces characters, and not strings... i couldn't do String a = a.replace('a',"ab");
Before attempting to write my own method, i decided to search for one already written... i'm pleased to announce, i found one! =) [ya, i'm lazy] This code was posted on another forum: 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;
}
basically use the method like this: replace(original_string, search_string, replace_with); thus, for my purposes, i will do: replace("I'm sure this works","\'","\\\'"); -------- credit is given to "idarke" from the tek-tips.com forums http://www.tek-tips.com/gviewthread.../269/qid/657910 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Java: addSlashes() ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|