Solved

remove the last character from a string

Posted on 2007-11-29
8
41,628 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now