Solved

remove the last character from a string

Posted on 2007-11-29
8
41,641 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 49
factorial example 4 39
servlet example 11 40
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 33
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

776 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