Results 1 to 7 of 7
  1. #1
    janjong03 is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default Help in substring and indexOf function

    Hi,

    Im using the subscript method to get a part of my string
    Java Code:
    String name = "John Paul Cruz"
    System.out.println("your first name is "+name.substring(0,name.indexOf());;
    output:
    Java Code:
    John
    i want to get output "John Paul" which is the first name
    Last edited by janjong03; 06-16-2010 at 05:09 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by janjong03 View Post
    Hi,

    Im using the subscript method to get a part of my string
    Java Code:
    String name = "John Paul Cruz"
    System.out.println("your first name is "+name1.substring(0,name1.indexOf());;
    output:
    Java Code:
    John
    i want to get output "John Paul" which is the first name
    That is not the code you were running. Nevermind, have a look at the String class API again and pay special attention to the lastIndexOf( ... ) method (assuming that a last name doesn't contain space characters).

    kind regards,

    Jos

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    name.substring(0,name.indexOf())
    Print out the value of name.indexOf() when you are getting unexpected results.
    Then read the API doc for String.substring() to see that the value returned by name.indexOf() is used for.

  4. #4
    janjong03 is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    But how will i do it if im gonna get the string from the user?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by janjong03 View Post
    But how will i do it if im gonna get the string from the user?
    Did you read my reply? b.t.w. the indexOf method (as well as the lastIndexOf method) has a parameter. What you showed doesn't have such a method.

    kind regards,

    Jos

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    But, "John Paul" isn't their first name, is it?

  7. #7
    janjong03 is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    @ josah. Sorry about that, now i got it. Thanks for the help

Similar Threads

  1. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  2. String indexOf
    By ras_pari in forum Advanced Java
    Replies: 3
    Last Post: 10-07-2009, 07:33 AM
  3. String substring function
    By ravian in forum New To Java
    Replies: 6
    Last Post: 01-02-2008, 07:35 PM
  4. tricky indexOf implementation -- Help!!
    By definewebsites in forum New To Java
    Replies: 3
    Last Post: 12-10-2007, 12:48 AM
  5. Help regarding indexOf
    By gauravj in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 01:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •