getting characters out of a string

Hi,

I am trying to compare a users IP address to the servers IP address, but I only want to compare the 1st 7 characters of the users IP address.

I wrote this to print out 2 strings:

<%

String userIP = request.getRemoteAddr();

char[] check = new char[7];

userIP.getBytes( 0, 6, check, 0);

String s = check.toString();

out.write ( "your ip is = " + userIP + "<br><br>" );

out.write ( "first 7 char are = " + s + "<br><br>" );

%>

"String userIP" shows the users IP address (ie 123.456.789.123) and "String s" should show the 1st 7 characters of the users IP (ie 123.456), but its not printing that, its showing something else...what i am lacking?

Thank,

Brent

[773 byte] By [brentgarner] at [2007-9-27 22:26:08]
# 1

oops, looks like I copied my code before I checked it, it should be this:

<%

String userIP = request.getRemoteAddr();

char[] check = new char[7];

userIP.getChars( 0, 6, check, 0);

String s = check.toString();

out.write ( "your ip is = " + userIP + "<br><br>" );

out.write ( "first 7 char are = " + s + "<br><br>" );

%>

sorry about that :)

brentgarner at 2007-7-7 12:55:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Much easier if you use startsWith() and substring(). If I have a string "abcdef" named x, then x.substring(0,3)will yield "abc". If I want to find out if x has "abc" as it's first characters then I just test: x.startsWith("abc")
cgarethc at 2007-7-7 12:55:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...