可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm trying to make a little chat program for experimentation, but seeing as I'm not the best Java programmer, I don't know how to separate a port from an IP where they are both in the same string.
This isn't super clear, but here's basically what I want to do.
User enters IP and port in IP:Port format
Scanner grabs it and puts it into a String
Somehow put everything before the colon into a string and all numbers after the colon into an int.
Any ideas on how to do this?
回答1:
First, you should check if the String
contains a colon. Then, you can use String.split(String)
and Integer.parseInt(String)
with something like
String input = "127.0.0.1:8080"; // <-- an example input
int port = 80; // <-- a default port.
String host = null;
if (input.indexOf(':') > -1) { // <-- does it contain ":"?
String[] arr = input.split(":");
host = arr[0];
try {
port = Integer.parseInt(arr[1]);
} catch (NumberFormatException e) {
e.printStackTrace();
}
} else {
host = input;
}
System.out.printf("host = %s, port = %d%n", host, port);
Output is
host = 127.0.0.1, port = 8080
回答2:
public static void main(String args[]){
String allTogether= "ip:port";
String[] array;
if(allTogether.contains(":")){
array = allTogether.split(":");
String ip = array[0];
String port = array[1];
System.out.println(ip);
System.out.println(port);
}
}
回答3:
to validate IP:PORT format you may try this:
public class Validator {
private static final String IP_PORT_PATTERN =
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5]):" +
"([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$";
private static final Pattern PATTERN;
static {
PATTERN = Pattern.compile(IP_PORT_PATTERN);
}
public static boolean validate(final String s) {
return PATTERN.matcher(s).matches();
}
}
thanks to frb and mkyong :)
and to separate ip and port :
public static void main(String args[]) {
String s = "127.0.0.1:8080";
if (validate(s)) {
String ip = s.substring(0, s.indexOf(':'));
int port = Integer.parseInt(s.substring(s.indexOf(':') + 1));
System.out.println(ip);
System.out.println(port);
} else {
System.out.println("invalid format");
}
}
回答4:
Use String.split
IP = splittedIP[0], Port = splittedIP[1] in String
you will need Integer.parseInt to get the integer value of port
String[] ipSplit = "127.0.0.1:80".split(":");
String ip = ipSplit[0];
int port = Integer.parseInt(ipSplit[1]);
回答5:
String s = "127.0.0.1:999";
String[] parts = s.split(":");
String address = parts[0];
int port = Integer.parseInt(parts[1]);
In this case, address
will be "127.0.0.1"
and port
will be the int
999
.
Note that Integer.parseInt
will throw a NumberFormatException
in this case if the portion of the string after the :
cannot be parsed as an integer, as in "127.0.0.1:blahblah"
. As well, if the string does not contain a :
there will be no parts[1]
.
回答6:
You can use pattern matcher in java to get the address and the port information and by the way you can validate the input string as well.
Pattern pattern = Pattern.compile(REGEX_FOR_IPADDRESS_WITH_PORT);
Matcher matcher = pattern.matcher("127.0.0.1:80");
if (matcher.matches()) {
System.out.printf("host = %s, port = %s%n", matcher.group(1), matcher.group(2));
} else {
System.out.println("Invalid Ip Address");
}
You can have multiple regular expression to validate V4 and V6 addresses.
Hope this would help.