how the indexof method is realy implemented

Can please sombody please help me Iam a student of nottingham university and would like know how the java method indexof(String ) wich searches for a given substring in a string is accually implemented.Well at least how the algorithm works
[253 byte] By [suren685] at [2007-9-26 11:54:01]
# 1
System.out.println("Testing to find word by method indexOf".indexOf("word"));// index is 16
masuda1967 at 2007-7-2 2:11:31 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Here ya go. This is the actual java implementation of both String indexOf(...) methods, as found in JDK 1.2. Note that the 'count' is an int variable showing the length of the string and the 'value' variable references the actual char[] array that houses the string.

Hope that helps...

/**

* Returns the index within this string of the first occurrence of the

* specified substring. The integer returned is the smallest value

* <i>k</i> such that:

* <blockquote><pre>

* this.startsWith(str, <i>k</i>)

* </pre></blockquote>

* is <code>true</code>.

*

* @paramstrany string.

* @return if the string argument occurs as a substring within this

* object, then the index of the first character of the first

* such substring is returned; if it does not occur as a

* substring, <code>-1</code> is returned.

* @exception java.lang.NullPointerException if <code>str</code> is

* <code>null</code>.

*/

public int indexOf(String str) {

return indexOf(str, 0);

}

/**

* Returns the index within this string of the first occurrence of the

* specified substring, starting at the specified index. The integer

* returned is the smallest value <i>k</i> such that:

* <blockquote><pre>

* this.startsWith(str, <i>k</i>) && (<i>k</i> >= fromIndex)

* </pre></blockquote>

* is <code>true</code>.

* <p>

* There is no restriction on the value of <code>fromIndex</code>. If

* it is negative, it has the same effect as if it were zero: this entire

* string may be searched. If it is greater than the length of this

* string, it has the same effect as if it were equal to the length of

* this string: <code>-1</code> is returned.

*

* @paramstr the substring to search for.

* @paramfromIndexthe index to start the search from.

* @return If the string argument occurs as a substring within this

* object at a starting index no smaller than

* <code>fromIndex</code>, then the index of the first character

* of the first such substring is returned. If it does not occur

* as a substring starting at <code>fromIndex</code> or beyond,

* <code>-1</code> is returned.

* @exception java.lang.NullPointerException if <code>str</code> is

* <code>null</code>

*/

public int indexOf(String str, int fromIndex) {

char v1[] = value;

char v2[] = str.value;

int max = offset + (count - str.count);

if (fromIndex >= count) {

if (count == 0 && fromIndex == 0 && str.count == 0) {

/* There is an empty string at index 0 in an empty string. */

return 0;

}

/* Note: fromIndex might be near -1>>>1 */

return -1;

}

if (fromIndex < 0) {

fromIndex = 0;

}

if (str.count == 0) {

return fromIndex;

}

int strOffset = str.offset;

char first = v2[strOffset];

int i = offset + fromIndex;

startSearchForFirstChar:

while (true) {

/* Look for first character. */

while (i <= max && v1[i] != first) {

i++;

}

if (i > max) {

return -1;

}

/* Found first character, now look at the rest of v2 */

int j = i + 1;

int end = j + str.count - 1;

int k = strOffset + 1;

while (j < end) {

if (v1[j++] != v2[k++]) {

i++;

/* Look for str's first char again. */

continue startSearchForFirstChar;

}

}

return i - offset;/* Found whole string. */

}

}

NiallB at 2007-7-2 2:11:31 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

import java.io.*;

import java.util.*;

class StringTokenizerDemo {

public static void main (String args[])throws Exception{

System.out.println("This program tokenizes a string with a given token\n");

System.out.println("For example the given string is \"FiveFillFiverFithiestFirst\"");

System.out.println("and the token is 'Fi' then this results in 've' 'll' 'ver' 'thiest' 'rst'");

System.out.println("Enter a string");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String myString = br.readLine().trim();

System.out.println("Enter the token");

String token = br.readLine().trim();

int count=0;

int i = myString.indexOf(token,count);

while((i!=-1)&&(count<myString.length())){

System.out.println(myString.substring(count,i));

count = i+token.length();

i = myString.indexOf(token,count);

}

if(i==-1)

System.out.println(myString.substring(count,myString.length()));

}

}

take a look at this simple program, you will understand.>

kksenji at 2007-7-2 2:11:31 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

http://forum.java.sun.com/thread.jsp?forum=54&thread=186531

take a look at this thread too

import java.io.*;

import java.util.*;

class StringTokenizerDemo {

public static void main (String args[])throws Exception{

System.out.println("This program tokenizes a string with a given token\n");

System.out.println("For example the given string is \"FiveFillFiverFithiestFirst\"");

System.out.println("and the token is 'Fi' then this results in 've' 'll' 'ver' 'thiest' 'rst'");

System.out.println("Enter a string");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String myString = br.readLine().trim();

System.out.println("Enter the token");

String token = br.readLine().trim();

int count=0;

int i = myString.indexOf(token,count);

while((i!=-1)&&(count<myString.length())){

System.out.println(myString.substring(count,i));

count = i+token.length();

i = myString.indexOf(token,count);

}

if(i==-1)

System.out.println(myString.substring(count,myString.length()));

}

}

>

kksenji at 2007-7-2 2:11:31 > top of Java-index,Java HotSpot Virtual Machine,Specifications...