Results 1 to 8 of 8
Thread: Searching the first occurence
- 11-27-2009, 11:41 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Searching the first occurence
Hi everybody,
i should write a methode, which gives the first occurence of a substring in a string with an index.
If the string starts with the substring so the index must be 0, if the substring does not exist in the string so the index is -1. If the substring.length is 0 so the index is 0.
The method is:
The strings are changed into the array. In this method we are working with arrays which have charakters. (I've already wrote the methode to change strings into the array...)Java Code:public static int getFirstOccurence(char[] str, char[] substr){ //TO DO }
Can anybody help me? I don't know how to search an array, in an other array?
- 11-27-2009, 02:06 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,425
- Blog Entries
- 7
- Rep Power
- 17
- 11-28-2009, 10:07 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Thank you for your answer,
but this is an homework and i should do it with arrays. Is there any other ideas, or some tipps?
- 11-28-2009, 10:47 PM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
So we are solving your homework?
please, learn something...
but anyway make a for loop for al the charachter
check if the character is the first character of the searched string
if so, check if the next charachter of the string is the next character of the searched string
(another for loop)
hope this helps and that you solve youre homework by yourself
- 11-28-2009, 10:54 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
thanks dinosep,
i don't want you to solve my homework, it's only a little part of whole homework. I couln't do it. I needed some help. Every tipp can be really helpfull. :)
i'll try your tip. thanks again :)
- 11-29-2009, 10:24 AM #6
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
ok,tell me if it works.
- 11-29-2009, 12:13 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,425
- Blog Entries
- 7
- Rep Power
- 17
The OP can smuggle a bit: look in the directory where the JDK is stored; it contains a file src.zip; it contains the source code for all the publicly avaialable classes. The source for the String class is also in there and that class contains the indexOf( ... ) method. Don't tell them I told you ;-)
kind regards,
Jos
- 11-29-2009, 12:36 PM #8
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
Similar Threads
-
Searching XML file using DOM
By simon in forum XMLReplies: 5Last Post: 03-30-2009, 01:27 AM -
count occurence of word in a line of text
By sinyi88 in forum New To JavaReplies: 19Last Post: 02-28-2009, 07:37 AM -
Problem with searching while using GUI
By BHCluster in forum AWT / SwingReplies: 1Last Post: 04-24-2008, 06:56 PM -
Searching an arraylist
By adelgado0723 in forum New To JavaReplies: 1Last Post: 04-15-2008, 01:09 PM -
searching
By nalinda in forum New To JavaReplies: 3Last Post: 12-06-2007, 02:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks