how to eliminate quotes in a string ... e.g. String str= "abc\"def";

B

bronby

please help...

Given: String str1= "abc\"def";
String str2= "\"1234\"";

Required: String str1 = "abcdef";
String str2 = "1234";

Thanks in advance..
 
F

Filip Larsen

Given: String str1= "abc\"def";
String str2= "\"1234\"";

Required: String str1 = "abcdef";
String str2 = "1234";

You have several implementation options that mainly fall into two
catagories: Code a loop that scans throught the characters of the string
and build up a new string without the quotes, or use regular
expressions. The loop is good if you want to control resources (CPU and
RAM) spend solving the problem, and the regex is good if you just want
to write it simple.

If you use Java 1.5, the regex can be done like:

String str1 = " .... ";
String str1WithoutQuotes = str1.replaceAll("\"","");

In Java 1.4 the same effect can be achieved by

String str1WithoutQuotes =
java.util.regex.Pattern.compile("\"").matcher(str1).replaceAll("");

If you have plenty of those I recommend you put it into a convenience
method somewhere like

private static final java.util.regex.Pattern QUOTE_PATTERN =
Pattern.compile("\"");

public static String removeQuotes(String s) {
return QUOTE_PATTERN.matcher(s).replaceAll("");
}



Regards,
 
T

Thomas Kellerer

You have several implementation options that mainly fall into two
catagories: Code a loop that scans throught the characters of the string
and build up a new string without the quotes, or use regular
expressions. The loop is good if you want to control resources (CPU and
RAM) spend solving the problem, and the regex is good if you just want
to write it simple.

If you use Java 1.5, the regex can be done like:

String str1 = " .... ";
String str1WithoutQuotes = str1.replaceAll("\"","");

In Java 1.4 the same effect can be achieved by

String str1WithoutQuotes =
java.util.regex.Pattern.compile("\"").matcher(str1).replaceAll("");

String str1WithoutQuotes = str1.replaceAll("\"","");

will work with 1.4 as well.

Thomas
 
R

Roedy Green

please help...

Given: String str1= "abc\"def";
String str2= "\"1234\"";

Required: String str1 = "abcdef";
String str2 = "1234";

I assume you are not complaining about the use of \ and you want to
avoid dealing with that, but that you want to get rid of " in a
string. You get rid of it the same way you would any other character.

It may be easier to follow what I am doing if I want to get rid of all
the Zs. I trust you can deal with converting every Z to \" when you
write the code.

if ( text == null )
{
// nothing to do.
return null;
}

if ( text.indexOf( 'Z' ) < 0 )
{
// nothing to do.
return text;
}

StringBuilder sb = new StringBuilder( text.length );

for ( int i=0; i<text.length(); i++ )
{
char c = text.charAt( i );
if ( c != 'z' )
{
sb.append( c );
}
}
return sb.toString();



--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
R

Roedy Green

F

Filip Larsen

Filip Larsen wrote
If you use Java 1.5, the regex can be done like:

String str1 = " .... ";
String str1WithoutQuotes = str1.replaceAll("\"","");

In Java 1.4 the same effect can be achieved by

String str1WithoutQuotes =
java.util.regex.Pattern.compile("\"").matcher(str1).replaceAll("");

I dont know why I got the idea that String.replaceAll() first appeared
in 1.5.
String.replaceAll() has of course been there since 1.4.


Regards,
 
B

bronby

String str1WithoutQuotes = str1.replaceAll("\"","");

thanks, this is what i need...
thanx evry1
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top