Formatting IP:Port string to

前端 未结 6 640
遇见更好的自我
遇见更好的自我 2021-01-23 07:13

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 bo

相关标签:
6条回答
  • 2021-01-23 07:58

    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
    
    0 讨论(0)
  • 2021-01-23 07:59

    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]);
    
    0 讨论(0)
  • 2021-01-23 08:01
    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].

    0 讨论(0)
  • 2021-01-23 08:03
    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);
            }
        }
    
    0 讨论(0)
  • 2021-01-23 08:05

    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");
        }
    }
    
    0 讨论(0)
  • 2021-01-23 08:12

    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.

    0 讨论(0)
提交回复
热议问题