basic question re: comparing chars

D

denim_genes

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.
 
D

denim_genes

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.

Just to clarify: I know what I should be doing, logically. I just
don't know the syntax (how to give it the commands).
 
K

Knute Johnson

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.

String.endsWith("/")
 
E

Eric Sosman

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!"
 
R

RichT

... 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 :)
 
L

Lew

RichT said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top