• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help in deleting last character of string.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone have a way to delete the last character in a string?
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
reply
    Bookmark Topic Watch Topic
  • New Topic