basic question re: comparing chars

Discussion in 'Java' started by denim_genes@hotmail.com, Mar 13, 2008.

  1. Guest

    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.
    , Mar 13, 2008
    #1
    1. Advertising

  2. Guest

    On Mar 13, 4:50 pm, wrote:
    > 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).
    , Mar 13, 2008
    #2
    1. Advertising

  3. wrote:
    > 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("/")

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Mar 13, 2008
    #3
  4. Eric Sosman Guest

    wrote:
    > On Mar 13, 4:50 pm, wrote:
    >> 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).


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

    --
    Eric Sosman, Mar 13, 2008
    #4
  5. RichT Guest

    >
    > ... 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 :)
    RichT, Mar 13, 2008
    #5
  6. Lew Guest

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

    --
    Lew
    Lew, Mar 14, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Engineer
    Replies:
    6
    Views:
    599
    Jeremy Bowers
    May 1, 2005
  2. Replies:
    0
    Views:
    412
  3. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,239
    Tim Rentsch
    Sep 23, 2005
  4. Hongyu
    Replies:
    9
    Views:
    881
    James Kanze
    Aug 8, 2008
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    209
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page