This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Can I delete a substring?  RSS feed

 
Ranch Hand
Posts: 70
  • 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?
 
Ranch Hand
Posts: 225
  • 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
 
lowercase baba
Posts: 12746
51
Chrome Java Linux
  • 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".
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!