File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I delete a substring?

 
Fritz Largosa
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a function String#replaceAll(String, String) since JDK 1.4.

Otherwise, you can use indexOf and substring methods
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic