This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Need help in deleting last character of string. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help in deleting last character of string." Watch "Need help in deleting last character of string." New topic
Author

Need help in deleting last character of string.

Michael Domin
Greenhorn

Joined: Oct 12, 2004
Posts: 1
Anyone have a way to delete the last character in a string?
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Originally posted by Michael Domin:
Anyone have a way to delete the last character in a string?


Use substring(int beginIndex, int endIndex) method.
The beginIndex should be zero and
the endIndex should be the length of the string minus one.


Mani
Quaerendo Invenietis
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

Strings in Java are immutable - they cannot be changed.

Having said that, what you CAN do is get a substring of a given string, and assign it to a (possibly new) variable...

so something like

String fullString = "remove last lettera"
String truncatedString = ???// get the substring of the above...

you can look at the java api and the String class for something that might help. if you knew the full length of the string, you'd just take the substring from the beginning to the last-but-one position...

API is here...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Anders Engstr�m
Greenhorn

Joined: Jul 25, 2004
Posts: 4
The code you want should look something like this:

String originalString = "whatever.";
String changedString = originalString.subString(0,originalString.length()-1);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help in deleting last character of string.