• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in deleting last character of string.

 
Michael Domin
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone have a way to delete the last character in a string?
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Anders Engstr�m
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you want should look something like this:

String originalString = "whatever.";
String changedString = originalString.subString(0,originalString.length()-1);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic