Solved

remove the last character from a string

Posted on 2007-11-29
8
41,665 Views
Last Modified: 2013-11-23
Hi,

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!
0
Comment
Question by:phil8258
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20378856
           String str = "John";
            str = str.substring(0, str.length()-1);
            System.out.println(str);
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 20378858
example = example.substring(0, example.length()-1);
0
 

Author Closing Comment

by:phil8258
ID: 31411843
Great, thanks :O)
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

Author Comment

by:phil8258
ID: 20379063
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??
0
 
LVL 92

Expert Comment

by:objects
ID: 20379076
example = example.substring(3);
0
 

Author Comment

by:phil8258
ID: 20379087
Brilliant, thanks again :O)
0
 
LVL 1

Expert Comment

by:cypher_punk
ID: 20581167
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());


Actually, i am attaching the information about method: 
string substring (start_index, end_index)
from sun site, this will help u understanding.
 
public String substring(int beginIndex,
                        int endIndex)
 
    Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
 
    Examples:
 
         "hamburger".substring(4, 8) returns "urge"
         "smiles".substring(1, 5) returns "mile"
         
 
    Parameters:
        beginIndex - the beginning index, inclusive.
        endIndex - the ending index, exclusive.
    Returns:
        the specified substring.
    Throws:
        IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
 
 
****For more info about string method, chk out this link:
http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html#substring(int,%20int)

Open in new window

0
 
LVL 1

Expert Comment

by:tukyunaaya
ID: 25653063
Using String Buffer

public class test {
	public static void main(String[] abc)
	{
		String s ="Testing Code";
		String reverse = removelast(s);
		System.out.println(reverse);
	}
	public static String removelast(String s) {
		StringBuffer buffer = new StringBuffer(s);  
		buffer = buffer.reverse();
		String  rev = buffer.toString();
		return rev;
	}
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question