example = example.substring(0, example.length()-1);
Main Topics
Browse All TopicsHi,
I have a string of unknown length, and i'm looking for a function that will remove the last character of it.
Like:
String example = "John";
example.removeLast();
example is now "Joh"
is this possible?
Cheers!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I'm now trying to do the same for the start of the word, like this:
example = example.substring(3, 0);
In order to remove the first 3 characters
so, JOHN whould become N
(not the same string as already removed end of though)
Unfortunately I get an exception: String index out of range: -3
What have I done wrong??
Hi,
To get the substring in java, there is a method called
object.substring (start index, last index);
So, if you want to remove the last character i.e. same as getting all the character except last one from the string.
e.g. String str = "john";
u can write the statement as:
str = str.substring (0, str.length()-1);
now, if you want to remove the first character i.e. same as getting all the character except the first one from the string
the statement would be
str = str.substring (1, str.length());
Business Accounts
Answer for Membership
by: UrosVidojevicPosted on 2007-11-29 at 16:57:33ID: 20378856
String str = "John";
str = str.substring(0, str.length()-1);
System.out.println(str);