• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

removing last occurence from string

 
madhu gadde
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the lastIndexOf method and the substring method on String and remove only the part that you would like.


 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to JiG Beginner.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic