File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can I  delete a substring? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can I  delete a substring?" Watch "Can I  delete a substring?" New topic
Author

Can I delete a substring?

Fritz Largosa
Ranch Hand

Joined: Sep 12, 2005
Posts: 70
I have a string "dog,cat,tree" I want to delete cat so it becomes "dog,tree". the replace function of string only accepts a char so method String_Object.replace(char,char) is not compatible with what I need which is a String_Object.replace(String,String). Any other alternative functions I can use?
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
There's a function String#replaceAll(String, String) since JDK 1.4.

Otherwise, you can use indexOf and substring methods


Regards,<br />Seb<br /> <br />SCJP 1.4
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

be carefull with strings. remember that you can't change a string once it is created.

your manipulation will create a NEW string, and unless you assign your reference to it, it will be lost.

in other words, if you do something like

String myString = new String("fred");
myString.toUpperCase();
System.out.println(myString);

the output will be "fred".

but if you do

myString = myString.toUpperCase();
System.out.println(myString);


you'll get "FRED".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I delete a substring?
 
Similar Threads
ClassCastException
Collection Example in K&B
enum semantics
Another examLab casting question
Best practice for resolving overloaded method names for Doc/Lit Wrapped