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 removing last occurence from string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "removing last occurence from string" Watch "removing last occurence from string" New topic

removing last occurence from string

madhu gadde

Joined: May 27, 2005
Posts: 11
Hi all,

anyone plz help me by solving the problem i faced.
In my application i need to remove only the last occurence of the character "," from the string.There are so many "," characters in that String.

Thanks in advance.
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
Use the lastIndexOf method and the substring method on String and remove only the part that you would like.

James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Or as an alternative use a regular expression

String string ="1,2,3,4,5,6,7,8,9,1,2,3,,4";
string = string.replaceAll("[,]([^,]*)$","$1");

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Moving to JiG Beginner.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: removing last occurence from string
It's not a secret anymore!