File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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

Joined: Oct 02, 2003
Posts: 11955

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");

the output will be "fred".

but if you do

myString = myString.toUpperCase();

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:
subject: Can I delete a substring?
It's not a secret anymore!