[Java] class String, method split

got-anna

I use method split, or class StringTokenizer to split a string, but they work too badly when I use them many times in various my own method. Why? Plz, show me.

rid2000

Show your problem in example.

got-anna

Here, an example, that does not work with split:

public class TestSplitIP {
public static void main(String[] args) {
String request = "REJECT 172.16.12.100";
String[] sip = request.split(" ");
System.out.println(sip[0] + "-" + sip[1]);
// result: REJECT-172.16.12.100
IP ip = new IP(sip[1]);
System.out.println(ip);
// result: IP: 0.0.0.0
// What shit is here?
}
}
class IP {
int[] ip = new int[4];
public IP(String str) {
System.out.println(str);
// result: 172.16.12.100
String[] sip = str.split(".");
System.out.println("sip.length = " + sip.length);
// result: sip.length = 0
// ?
for (int i = 0; i < sip.length; ++i) {
ip[i] = Integer.parseInt(sip[i]);
}
}
public String toString {
String tmp = "";
for (int i = 0; i < 4; ++i) {
tmp = tmp + ip[i] + i == 3) ? "" : ".");
}
return "IP: " + tmp;
}
}

ermsoft

 String[] sip = str.split("\\."); 

Кстати, почему квадратные скобки в теге CODE такие корявые?

got-anna

Hi, I'm using it wrong.
It's should be:

String[] sip = str.split("[.]");
Оставить комментарий
Имя или ник:
Комментарий: