Solved

remove the last character from a string

Posted on 2007-11-29
8
41,637 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 74
Which is the latest version of Apache Tomcatl 3 49
eclipse shortcuts 9 45
diffSum example 4 20
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

932 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

10 Experts available now in Live!

Get 1:1 Help Now