[487] in java-interest

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

Re: String.indexOf(String,int)

daemon@ATHENA.MIT.EDU (Jonathan Payne)
Fri Jun 23 21:09:20 1995

Date: Fri, 23 Jun 1995 17:24:57 -0700
From: jpayne@starwave.com (Jonathan Payne)
To: ekim@nyquist.bellcore.com
Cc: java-interest@java.sun.com
In-Reply-To: <199506232131.RAA29694@nyquist.bellcore.com> (ekim@nyquist.bellcore.com)

I thought there was something fishy there.

Your constructor of the String in the first place was wrong!

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

This says, Create me a String out of an array of bytes, and set the hi
byte of each character to buff.length.  This explains why your string
matches on str.substring(...) worked, since they, too, had the hi byte
set.  The constructor you want is:

    public String(byte ascii[], int hibyte, int offset, int count);

> From: ekim@nyquist.bellcore.com (Michael Mills 21728)
> Date: Fri, 23 Jun 1995 17:31:47 -0400 (EDT)
> X-Mailer: ELM [version 2.4 PL2]
> Mime-Version: 1.0
> Sender: owner-java-interest@java.sun.com
> Precedence: bulk
> X-Info: To unsubscribe, send 'unsubscribe' to java-interest-request@java.sun.com
> Content-Type: text/plain; charset=US-ASCII
> Content-Length: 2582
> 
> 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
> 
-
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