Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
bronby - 08 Jul 2005 06:51 GMT
please help...

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

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

Thanks in advance..
Thomas Kellerer - 08 Jul 2005 07:14 GMT
> please help...
>
[quoted text clipped - 5 lines]
>
> Thanks in advance..

Have a look at String.replaceAll()

<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replaceAll(java.la
ng.String,%20java.lang.String
)>

Thomas
Roedy Green - 08 Jul 2005 14:53 GMT
><http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replaceAll(java.la
ng.String,%20java.lang.String
)>

that is a fairly heavy duty tool for such a lightweight problem.

Signature

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

Filip Larsen - 08 Jul 2005 11:12 GMT
> 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,
Signature

Filip Larsen

Thomas Kellerer - 08 Jul 2005 11:29 GMT
> You have several implementation options that mainly fall into two
> catagories: Code a loop that scans throught the characters of the string
[quoted text clipped - 12 lines]
>   String str1WithoutQuotes =
> java.util.regex.Pattern.compile("\"").matcher(str1).replaceAll("");

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

will work with 1.4 as well.

Thomas
bronby - 11 Jul 2005 10:20 GMT
String str1WithoutQuotes = str1.replaceAll("\"","");

thanks, this is what i need...
thanx evry1
Filip Larsen - 08 Jul 2005 18:36 GMT
> If you use Java 1.5, the regex can be done like:
>
[quoted text clipped - 5 lines]
>   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,
Signature

Filip Larsen

Roedy Green - 08 Jul 2005 14:52 GMT
>please help...
>
[quoted text clipped - 3 lines]
>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();

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.