Removing a quote character

J

jagonzal

Mike said:
Ok, heres a tuff one:

How do I remove all quote characters, i.e, " from a string?

suppose you string with quote characters is "aStringWithQuotes":

String aStringWithNoQuotes = aStringWithQuotes.replaceAll("\"","");
 
R

Roedy Green

How do I remove all quote characters, i.e, " from a string?

check out he String.replace method see
http://mindprod.com/jgloss/string.html

if you want to roll your own, use a StringBuilder and append the non-"
chars one by one picked out with charAt.

If you want to get fancy, count the non "s, if 0 return the original
string. If > 0 allocate a char[] array just the right size for the
non-"s. Then convert than to String when done.
 
R

Roedy Green

String aStringWithNoQuotes = aStringWithQuotes.replaceAll("\"","");

You don't replaceAll, just plain replace. ReplaceAll is the regex
version. You want the simple replace( String, String). You can't use
the even faster replace( char, char ) since your you are replacing
with nothing not a char.
 
O

Oliver Wong

Roedy Green said:
You don't replaceAll, just plain replace. ReplaceAll is the regex
version. You want the simple replace( String, String). You can't use
the even faster replace( char, char ) since your you are replacing
with nothing not a char.

It's an unfortunate choice for method name, as when I took at look at
jagonzal's code snippet, I didn't immediately notice anything wrong. They
should have named it "regExpReplace" or something more obvious.

- Oliver
 
M

Mike

Ok, heres a tuff one:

How do I remove all quote characters, i.e, " from a string?


Thanks Guys,

replaceAll worked for me where as "replace" did not.

Coding jsps running on tomcat 4. Tomcat kept giving me an error with
simply "replace"

Again thanks, I was really stuck, now I can move on.
 
R

Roedy Green

replaceAll worked for me where as "replace" did not.

did you try to replace( '\"', "" ) ?? What did you try and what do you
mean by did not work. You got a compile error. the string had no
change, some other change?
 
T

tom fredriksen

suppose you string with quote characters is "aStringWithQuotes":

String aStringWithNoQuotes = aStringWithQuotes.replaceAll("\"","");

I noticed that the string class has a couple of methods that the Matcher
class has, namely replaceAll() and replaceFirst().
Does the String class use the Matcher class internaly since it can
accept regular expressions or does it use a private mechanism?
I would think the first is the correct one?

/tom
 
R

Roedy Green

I noticed that the string class has a couple of methods that the Matcher
class has, namely replaceAll() and replaceFirst().
Does the String class use the Matcher class internaly since it can
accept regular expressions or does it use a private mechanism?
I would think the first is the correct one?

You can answer those questions by looking at the source in src.zip.
Most IDEs will show it two you in a keystroke or two.

It will show you the code:

public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);


You can see String.replaceAll is just a convenience wrapper that
re-compiles the Pattern every time you use it.
 
O

Oliver Wong

Roedy Green said:
You can answer those questions by looking at the source in src.zip.
Most IDEs will show it two you in a keystroke or two.

It will show you the code:

public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);


You can see String.replaceAll is just a convenience wrapper that
re-compiles the Pattern every time you use it.

Unless this is documented in the API though, you should not rely on this
fact; Sun may change the implementation at a later date.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top