• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Remove Chars from a String?

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I remove chars from a string.



if some one can give me an idea?

Reg
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

use string method ->replaceFrist or replaceAll

Hope This Helps
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ummm... isn't the substring() method enough by itself?

To remove first two chars:

To remove last two chars:

Of course for robust code you prbably want to make sure that there really are at least two chars in the string, before you remove them.
 
Reggie McDougal
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works well would you say its overdoing it? Do I need to use the string buffer? Is s = s.substring(2); using StringBuffer under the hood?

Reg

 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would you say its overdoing it?

Yes.

Do I need to use the string buffer?

No.

Is s = s.substring(2); using StringBuffer under the hood?

No.
[ January 12, 2005: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And of course you're not removing anything from the String. You're creating a new String that's the old String minus the stuff you want removed.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Reggie McDougal:
This works well would you say its overdoing it? Do I need to use the string buffer? Is s = s.substring(2); using StringBuffer under the hood?

Reg


Perhaps this is overdoing it if you only want to remove the first 2 chars (or the last 2). However, if you want to remove chars from the middle of a String, this is probably better than trying to use substring().

Layne
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!