Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

basic question re: comparing chars

Thread view: 
denim_genes@hotmail.com - 13 Mar 2008 21:50 GMT
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.
denim_genes@hotmail.com - 13 Mar 2008 21:51 GMT
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

Knute Johnson - 13 Mar 2008 22:15 GMT
> 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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.