wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Remove last character from a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Remove last character from a string" Watch "Remove last character from a string" New topic
Author

Remove last character from a string

Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
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


Mike<br />SCJP 1.4<br />----------------------------<br />mdcleary@earthlink.net<br />----------------------------<br />There are 10 types of people<br />in the world. Those that <br />understand binary, and those<br />that don't.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Jul 29, 2003
Posts: 93
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove last character from a string
 
Similar Threads
Remove last character from String
removing last occurence from string
How to remove '\0'
Flow control
How to remove Numeric values from a String