1.判断此字符串是否是一个IP地址
public static boolean isIp(String IP) {
boolean b = false;
while (IP.startsWith(" ")) {
IP = IP.substring(1, IP.length()).trim();
}
while (IP.endsWith(" ")) {
IP = IP.substring(0, IP.length() - 1).trim();
}
if (IP.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")) {
String s[] = IP.split("\\.");
if (Integer.parseInt(s[0]) < 255)
if (Integer.parseInt(s[1]) < 255)
if (Integer.parseInt(s[2]) < 255)
if (Integer.parseInt(s[3]) < 255)
b = true;
}
return b;
}
2.判断此字符串是否包含数字
public static boolean isContainNumber(String content) {
boolean flag = false;
Pattern p = Pattern.compile(".*\\d+.*");
Matcher m = p.matcher(content);
if (m.matches()) {
flag = true;
}
return flag;
}
3.随机生成一个IP地址
private static String generateRandomIp(){
String ip = "";
Random random = new Random();
for (int j = 0; j < 4; j++) {
int randomIpNum = random.nextInt(256);
ip += j == 0 ? randomIpNum : "." + randomIpNum;
}
return ip;
}
4.list内的字符串排序
private static void sortList(List<String> list) {
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}
});
}
// 升序
Collections.sort(list);
// 降序
Collections.sort(list,Collections.reverseOrder());
JAVA交流群:260052172(只分享,不解决)