Quick Java question

Hi all, just quick Q concerning Java. Basically, how do i chop a character off the end of a String?

This will chop the last character off a String.
Code:
String s = "abc";
	
if (s.length() > 0) {
    s = s.substring(0, s.length()-1);
}


Trim only takes off white space doesn't it?
Yup. The java API docs are a great reference for checking things like this.

Here are the entries for trim and substring :)
 
Last edited:
You can also use String.isEmpty() to check for a zero length string since Java 6. I don't know why but they seem to have added a lot of new methods to existing classes in the latest relase that are quite trivial, I guess it's just to make the developers life easier. Methods like isEmpty() exist for many other classes, there isn't really much consistency over the whole API but it's got much better over the past few versions.
 
Back
Top Bottom