End of String Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scoobyhcg
    New Member
    • Mar 2008
    • 1

    End of String Question

    I have what I hope is a simple question but am seriously stuck...

    Supposed to write a program that will ask the user for an address and then output some data...

    I have part of the code working properly. Where I am stuck is that I need to tell the program to look at the last 5 characters in the string and output it...

    Can anyone help? I know it has something to with length, indexof or substring but can't seem to figure out how to do it... any help is appreciated!!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Just think a bit; e.g. what does this do:

    [code=java]
    String s= "a test string";
    int n= 5;
    String q= s.substring(s.l ength()-n);
    [/code]

    Check all the methods in the API documentation for the String class.

    kind regards,

    Jos

    Comment

    • sujansarathi
      New Member
      • Mar 2008
      • 6

      #3
      Could you please send the code ?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by sujansarathi
        Could you please send the code ?
        You did read reply #2 didn't you?

        kind regards,

        Jos

        Comment

        • sujansarathi
          New Member
          • Mar 2008
          • 6

          #5
          Originally posted by JosAH
          You did read reply #2 didn't you?

          kind regards,

          Jos

          Sorry by mistake i sent it to u, its for the user who asked the question.

          Comment

          • sujansarathi
            New Member
            • Mar 2008
            • 6

            #6
            Originally posted by scoobyhcg
            I have what I hope is a simple question but am seriously stuck...

            Supposed to write a program that will ask the user for an address and then output some data...

            I have part of the code working properly. Where I am stuck is that I need to tell the program to look at the last 5 characters in the string and output it...

            Can anyone help? I know it has something to with length, indexof or substring but can't seem to figure out how to do it... any help is appreciated!!

            Could you please send the code ?

            Comment

            • tburger
              New Member
              • Jul 2007
              • 58

              #7
              JosAH left a great hint for you...

              Additionally, if you would like to be able to access any set of characters within the string, the Java String class also has a getChars() method that will allow you to convert the String input into an array of characters.

              i.e. the string "Hello" gets broken into

              char[] = {"H", "e","l","l","o" }

              Then you can print out any particular character by accessing the
              appropriate array index...

              For your scenario, however, I would certainly look into JosAH's method...

              Let us know if you're still having trouble,

              Tom

              Comment

              Working...