字符串判断方法

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(只分享,不解决)

 

转载于:https://my.oschina.net/Tsher2015/blog/903841

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值