[481] in java-interest

home help back first fref pref prev next nref lref last post

String.indexOf(String,int)

daemon@ATHENA.MIT.EDU (Michael Mills 21728)
Fri Jun 23 18:01:38 1995

From: ekim@nyquist.bellcore.com (Michael Mills 21728)
To: java-interest@java.sun.com
Date: Fri, 23 Jun 1995 17:31:47 -0400 (EDT)

Hi,


I have been playing with the string class and I just can't figure this
out.  I expected the output to be:
TEST!
CONVERTED 65535
findme
findmex
THE FIRST = 6000
THE FIRST = 6000
THE FIRST = 6000
THE FIRST = 6000
THE FIRST = 6000

but got:

TEST!
CONVERTED 65535
findm
findme
THE FIRST = 6000
THE FIRST = -1
THE FIRST = -1
THE FIRST = 6000
THE FIRST = -1

1) Should str.substring(6000,6005) produce a 5 char string or a 6 char string
2) What is wrong with the calls to indexOf that return -1?

Here is the program:

import java.lang.system;
import java.lang.Object;
import java.io.InputStream;
import net.www.http.HttpClient;
import net.www.html.URL;
class trySearch {
        public static void main (String argv [])
        {
                System.out.println("TEST!");
                byte buff[] = new byte[65535];

                for(int x=0;x<65535;x++)
                {
                        buff[x] = 'x';
                }

                buff[6000] = 'f';
                buff[6001] = 'i';
                buff[6002] = 'n';
                buff[6003] = 'd';
                buff[6004] = 'm';
                buff[6005] = 'e';

                String str = new String(buff,buff.length);


                System.out.println("CONVERTED "+str.length() );
                System.out.println(str.substring(6000,6005)) ;
                System.out.println(str.substring(6000,6006)) ;



                int first = str.indexOf(str.substring(6000,6005),0);
                System.out.println("THE FIRST = " + first);

                first = str.indexOf("find",0);
                System.out.println("THE FIRST = " + first);

                String search = new String("find");
                first = str.indexOf(search,0);
                System.out.println("THE FIRST = " + first);



                search = str.substring(6000,6005);
                first = str.indexOf(search,0);
                System.out.println("THE FIRST = " + first);

                byte buff2[] = new byte[5];
                buff2[0] = 'f';
                buff2[1] = 'i';
                buff2[2] = 'n';
                buff2[3] = 'd';
                buff2[4] = 'm';
                search = new String(buff2, buff2.length );
                first = str.indexOf(search,0);
                System.out.println("THE FIRST = " + first);
                System.out.println(search);
                System.out.println(str.substring(6000,6005) );
        }
}


Thanks,


Mike

-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post