Solved

remove the last character from a string

Posted on 2007-11-29
8
41,647 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java stored proc example 9 44
by zero exception 10 52
jboss wildfly 10.1 10 219
web project error add remove 1 27
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

792 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