Results 1 to 5 of 5
  1. #1
    Sdannenberg3 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default find index of string in another string

    I need to write a method that finds the index of a string 2, inside string 1. For example, if the strings ("mississippi", "ss") are passed, it would return the index of where "ss" is in the string "mississippi" So in this case it would return a 2.

    I cannot use the String.indexOf(String) method.

    Thanks!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

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

    Default

    Quote Originally Posted by Eranga View Post
    Use substring(). Read the Java API for more details.
    Better use the indexOf( ... ) method. The substring( ... ) method can't find anything, it only chops up another string.

    kind regards,

    Jos

  4. #4
    Sdannenberg3 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Better use the indexOf( ... ) method. The substring( ... ) method can't find anything, it only chops up another string.

    kind regards,

    Jos
    I would do this, but if you read my post you would read "I cannot use the String.indexOf(String) method."

    I initially had the SAME thought as you though! My professor is trying to make us think outside of the box a little and create our OWN methods to do some simple tasks that already have methods created for these tasks. I think I got it figured out though, if so I will mark as solved.

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

    Default

    Quote Originally Posted by Sdannenberg3 View Post
    I would do this, but if you read my post you would read "I cannot use the String.indexOf(String) method."
    Oops, how silly of me, sorry about that; another cheat is to have a look at the source code for the indexOf( ... ) method (it's in the file src.zip stored in your JDK directory). It's a trivial method, no cleverness applied.

    kind regards,

    Jos

Similar Threads

  1. Find index position of every word in a String
    By pentace in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 08:26 PM
  2. Find a number from a string
    By florentp in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 09:01 PM
  3. Noob here, trying to access index in string
    By madmax2006 in forum New To Java
    Replies: 14
    Last Post: 03-08-2009, 05:08 PM
  4. Find java objects in a given string.
    By sarathi in forum New To Java
    Replies: 3
    Last Post: 03-06-2009, 08:42 AM
  5. Find and replace ( in a String
    By hamish10101 in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 05:51 AM

Posting Permissions

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