Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove last character from a string

 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you remove the last charater from a string? In a loop, certain characters get added to a string. But when the loop is finished, I need to remove the last character from the string.
Thanks,
Mike
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one way
String str = "Hello World+";
str = str.substring(0,str.length()-1);
if you're adding a delimiter in the loop, it might be easier to add the delimiter at the beginning, then
str = str.substring(1);
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent! Thank you. That is exactly what I need. I was trying to do something similar, but I can see that I wasn't close to the right syntax.
Thanks again!
Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic