I need to compare characters.
Basically, the user will input a String. I need to make sure the last
character of that string is "/".
How would I go about doing this? I mean in terms of the actual syntax
and character stuff.
On Mar 13, 4:50 pm, denim_ge...@hotmail.com wrote:
> I need to compare characters.
>
[quoted text clipped - 3 lines]
> How would I go about doing this? I mean in terms of the actual syntax
> and character stuff.
Just to clarify: I know what I should be doing, logically. I just
don't know the syntax (how to give it the commands).
Eric Sosman - 13 Mar 2008 22:21 GMT
> On Mar 13, 4:50 pm, denim_ge...@hotmail.com wrote:
>> I need to compare characters.
[quoted text clipped - 7 lines]
> Just to clarify: I know what I should be doing, logically. I just
> don't know the syntax (how to give it the commands).
The most direct way would be to pick out the String's
last character and compare it to a slash:
if (string.charAt(string.length()-1) == '/') ...
Unfortunately, this will fail on empty Strings, whose length
is zero. So you could add another condition:
if (string.length() > 0 &&
string.charAt(string.length()-1) == '/') ...
However, there's a *much* more convenient way:
if (string.endsWith("/")) ...
... and the moral of the story is "Use the Javadoc, Luke!"

Signature
Eric.Sosman@sun.com
RichT - 13 Mar 2008 22:44 GMT
> ... and the moral of the story is "Use the Javadoc, Luke!"
Ah all well and good if you know what you are looking for in the first
place and if JavaDoc had a search mechanism even better :)
Lew - 14 Mar 2008 02:03 GMT
>> ... and the moral of the story is "Use the Javadoc, Luke!"
>
> Ah all well and good if you know what you are looking for in the first
> place and if JavaDoc had a search mechanism even better :)
That'd help, but once you get used to the Javadocs it really isn't all that
bad. In this case, you know you want to find something in the String class
that helps find out if the String ends With something in particular. Browsing
through
<http://java.sun.com/javase/6/docs/api/java/lang/String.html>
in the "Method Summary" section will pretty quickly reveal what you want.
It is true that mastering the API docs is a life's work, and requires a
mystical librarian's power. So the sooner you get started, the better.

Signature
Lew
> I need to compare characters.
>
[quoted text clipped - 3 lines]
> How would I go about doing this? I mean in terms of the actual syntax
> and character stuff.
String.endsWith("/")

Signature
Knute Johnson
email s/nospam/linux/