Java判断字符串是否为IP地址的方法
作者:简简单单OnlineZuozuo 发布时间:2023-07-06 15:02:58
标签:java,字符串,IP地址
Java 判断字符串是否为IP地址,供大家参考,具体内容如下
1、代码
主要就是这么几个条件
非空
长度符合 0.0.0.0 - 255.255.255.255
包含分隔符 且 个数正确
四个全部是数字,且都在合理的范围内
/**
* 判断某个字符串是否是一个 IP 地址
*
* @param str 字符串
*/
public static boolean isIpStr(String str) {
// 非空
// boolean notBlank = StringUtils.isNotBlank(str);
// 长度符合 0.0.0.0 - 255.255.255.255
// boolean length = CommonUtils.isNumberBetween(str.length(),7,15);
if (StringUtils.isNotBlank(str) && CommonUtils.isNumberBetween(str.length(), 7, 15)) {
String regex = ".";
// 包含分隔符 且 个数正确
if (str.contains(regex) && str.split(regex).length == 4) {
boolean legalNumber = true;
// 四个全部是数字,且都在合理的范围内
for (String obj : Lists.newArrayList(str.split(regex))) {
if (NumberUtils.isDigit(obj)) {
Integer value = Integer.parseInt(obj);
legalNumber = CommonUtils.isNumberBetween(value, 0, 255);
} else {
// 任意一个不是数字,不合法
legalNumber = false;
break;
}
}
return legalNumber;
}
}
return false;
}
2、CommonUtils 工具类
package cn.zjcs.common.util;
import cn.hutool.core.util.ReUtil;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* @author Created by 谭健 on 2019/6/11. 星期二. 15:20.
* © All Rights Reserved.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CommonUtils {
/**
* 是否为 null
*
* @param o
* @return null返回 true
*/
public static boolean isNull(Object o) {
return o == null;
}
/**
* 是否不为 null
*
* @param o
* @return 不为 null 返回 true
*/
public static boolean isNotNull(Object o) {
return !isNull(o);
}
/**
* 是否是0 ,
*
* @param bigDecimal
* @return 0 返回true
*/
public static boolean isZeroDecimal(BigDecimal bigDecimal) {
return isNotNull(bigDecimal) && bigDecimal.compareTo(BigDecimal.ZERO) == 0;
}
/**
* 是否不是 0
*
* @param bigDecimal
* @return 不是0 返回true
*/
public static boolean isNotZeroDecimal(BigDecimal bigDecimal) {
return !isZeroDecimal(bigDecimal);
}
/**
* 是否是 1
*
* @param bigDecimal
* @return 是 1 返回true
*/
public static boolean isOneDecimal(BigDecimal bigDecimal) {
return isNotNull(bigDecimal) && bigDecimal.compareTo(BigDecimal.ONE) == 0;
}
/**
* 是否不是 1
*
* @param bigDecimal
* @return 不是 1 返回true
*/
public static boolean isNotOneDecimal(BigDecimal bigDecimal) {
return bigDecimal.compareTo(BigDecimal.ONE) != 0;
}
/**
* 是否是 0 long
*
* @param l
* @return 是 0 long 返回 true
*/
public static boolean isZeroLong(Long l) {
return l != null && l.equals(0L);
}
/**
* 是否不是 0 long
*
* @param l
* @return 不是 0 long 返回 true
*/
public static boolean isNotZeroLong(Long l) {
return !isZeroLong(l);
}
/**
* 是否是 0 int
*
* @param l
* @return 是 0 int 返回 true
*/
public static boolean isZeroInt(Integer l) {
return l != null && l.equals(0);
}
/**
* 是否不是 0 int
*
* @param l
* @return 不是 0 int 返回 true
*/
public static boolean isNotZeroInt(Integer l) {
return !isZeroInt(l);
}
/**
* 两个 decimal 是否相等
*
* @param i
* @param j
* @return 相等返回 true
*/
public static boolean isSameDecimal(BigDecimal i, BigDecimal j) {
return i.compareTo(j) == 0;
}
/**
* 第一个 decimal 是否大于 第二个 decimal
*
* @param i
* @param j
* @return 大于 返回true
*/
public static boolean isDecimalGt(BigDecimal i, BigDecimal j) {
return i.compareTo(j) > 0;
}
/**
* 第一个 decimal 是否小于 第二个 decimal
*
* @param i
* @param j
* @return 小于 返回true
*/
public static boolean isDecimalLt(BigDecimal i, BigDecimal j) {
return i.compareTo(j) < 0;
}
/**
* 特殊字符串处理
*
* @param character
* @return
*/
public static String replaceSpecialCharacter(String character) {
String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]";
return ReUtil.replaceAll(character, regEx, "");
}
/**
* 数据分比切割
* <p>
* 比如 p 为 2,要做千分切割,则 h 值为 "1000.00"
* 得到值为 0.002
*
* @param p 输入值
* @param h 切割值
* @return 切割后的值
*/
public static BigDecimal percentFormat(Integer p, String h) {
return new BigDecimal(String.valueOf(p)).divide(new BigDecimal(h), 4, RoundingMode.HALF_UP).setScale(4, BigDecimal.ROUND_HALF_UP);
}
public static boolean orEq(Object... o) {
if (o.length < 2) {
throw new NullPointerException("长度不足");
}
Object o1 = o[0];
for (int i = 1; i < o.length - 1; i++) {
if (o1.equals(o[i])) {
return true;
}
}
return false;
}
/**
* 包含边界值
*
* @param number 检查值
* @param min 最小
* @param max 最大
*/
public static boolean isNumberBetween(Number number, Number min, Number max) {
return number.longValue() >= min.longValue() && number.longValue() <= max.longValue();
}
/**
* 标准数学计算
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class Math {
/**
* 精确的表示分数的数学计算,因为使用double 等会丢失精度
*/
@SuppressWarnings("rawtypes")
@Getter
public static class Fraction extends Number implements Comparable {
private static final long serialVersionUID = 2330398718018182597L;
/**
* 定义分子
*/
private long numerator = 0;
/**
* 定义分母
*/
private long denominator = 1;
public Fraction() {
this(0, 1);
}
public Fraction(long numerator, long denominator) {
long gcd = gcd(numerator, denominator);
this.numerator = ((denominator > 0) ? 1 : -1) * numerator / gcd;
this.denominator = java.lang.Math.abs(denominator) / gcd;
}
/**
* 求最大公约数
*/
private long gcd(long f, long s) {
long fAbs = java.lang.Math.abs(f);
long sAbs = java.lang.Math.abs(s);
// 学术名称 Gcd
int _Gcd = 1;
// 欧几里德算法
for (int i = 1; i <= fAbs && i <= sAbs; i++) {
if (fAbs % i == 0 && sAbs % i == 0) {
_Gcd = i;
}
}
return _Gcd;
}
/**
* 分数的加法
*
*/
public Fraction add(Fraction secondRational) {
long n = numerator * secondRational.getDenominator() + denominator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Fraction(n, d);
}
/**
* 分数的减法
*
*/
public Fraction subtract(Fraction secondRational) {
long n = numerator * secondRational.getDenominator() - denominator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Fraction(n, d);
}
/**
* 分数乘法
*
*/
public Fraction mulitiply(Fraction secondRational) {
long n = numerator * secondRational.getNumerator();
long d = denominator * secondRational.getDenominator();
return new Fraction(n, d);
}
/**
* 分数除法
*
*/
public Fraction divide(Fraction secondRational) {
long n = numerator * secondRational.getDenominator();
long d = denominator * secondRational.numerator;
return new Fraction(n, d);
}
@Override
public String toString() {
if (denominator == 1) {
return numerator + "";
} else {
return numerator + "/" + denominator;
}
}
@SuppressWarnings("all")
@Override
public boolean equals(Object parm1) {
return (this.subtract((Fraction) (parm1))).getNumerator() == 0;
}
@Override
public int compareTo(Object o) {
if ((this.subtract((Fraction) o)).getNumerator() > 0) {
return 1;
} else if ((this.subtract((Fraction) o)).getNumerator() > 0) {
return -1;
} else {
return 0;
}
}
@Override
public double doubleValue() {
return numerator * 1.0 / denominator;
}
@Override
public float floatValue() {
return (float) doubleValue();
}
@Override
public int intValue() {
return (int) doubleValue();
}
@Override
public long longValue() {
return (long) doubleValue();
}
}
/**
* @param dividend 被除数
* @param divisor 除数
* @param accuracy 精度
*/
public static BigDecimal divide(BigDecimal dividend, BigDecimal divisor, int accuracy) {
// 0 除以任何数 = 无穷大,任何数除以 0 无法除,都会抛出错误
if (isZeroDecimal(divisor) || isZeroDecimal(dividend)) {
return BigDecimal.ZERO;
}
return dividend.divide(divisor, 16, RoundingMode.HALF_UP).setScale(accuracy, RoundingMode.HALF_UP);
}
/**
* @param f .
* @param s .
* @param accuracy 精度
*/
public static BigDecimal multiply(BigDecimal f, BigDecimal s, int accuracy) {
// 0 * 任何数 = 0
if (isZeroDecimal(f) || isZeroDecimal(s)) {
return BigDecimal.ZERO;
}
return f.multiply(s).setScale(accuracy, RoundingMode.HALF_UP);
}
/**
* 开多次方根
*
*/
public static BigDecimal pow(BigDecimal f, BigDecimal s) {
// 防止出现 Infinity 的情况
if (isZeroDecimal(f) && isDecimalLt(s, BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
return new BigDecimal(String.valueOf(java.lang.Math.pow(f.doubleValue(), s.doubleValue())));
}
/**
* 获取分数值
*
*/
public static BigDecimal fraction(Fraction f) {
long denominator = f.getDenominator();
long numerator = f.getNumerator();
return divide(new BigDecimal(String.valueOf(numerator)), new BigDecimal(String.valueOf(denominator)), 16);
}
}
}
3、NumberUtils 工具类
package cn.zjcs.common.util;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author ..
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class NumberUtils {
private static final Pattern DIGIT_PATTERN = Pattern.compile("[0-9]*");
/**
* 判断 某个 decimal 是否等于 0
*
* @param decimal BigDecimal 数字
* @return 等于0 返回 true
*/
public static boolean isZeroDecimal(BigDecimal decimal) {
return decimal == null || decimal.compareTo(BigDecimal.ZERO) == 0;
}
/**
* 判断 某个 decimal 是否不等于 0
*
* @param decimal BigDecimal 数字
* @return 不等于0 返回 true
*/
public static boolean isNotZeroDecimal(BigDecimal decimal) {
return decimal != null && decimal.compareTo(BigDecimal.ZERO) != 0;
}
/**
* 判断一个字符串是否是数字
*
* @param var 字符串
* @return 是数字返回 true
*/
public static boolean isDigit(String var) {
Matcher isNum = DIGIT_PATTERN.matcher(var);
return isNum.matches();
}
public static boolean isEmptyNumber(Number number) {
return number == null
|| number.intValue() == 0
|| number.longValue() == 0
|| number.doubleValue() == 0.00
|| number.byteValue() == 0
|| number.floatValue() == 0.0
|| number.shortValue() == 0;
}
public static boolean isNotEmptyNumber(Number number) {
return !isEmptyNumber(number);
}
public static boolean isNotZeroLong(Long something) {
if (something == null) {
return false;
}
return !something.equals(0L);
}
}
来源:https://blog.csdn.net/qq_15071263/article/details/107836450


猜你喜欢
- 本文实例为大家分享了WebView实现文件下载功能的具体代码,供大家参考,具体内容如下本节引言本节给大家介绍的是WebView下载文件的知识
- 线程封闭线程封闭一般通过以下三个方法:Ad-hoc线程封闭:程序控制实现,最糟糕,忽略堆栈封闭:局部变量,无并发问题ThreadLocal线
- 一、SpringBoot是什么Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以
- Android 7.0系统在运行应用的时候,对权限做了诸多限制,normal, dangerous, signature, signatur
- 这篇文章主要介绍了spring cloud gateway请求跨域问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定
- 译文链接: https://www.infoworld.com/art...AutoMapper 是一个非常流行的 object-to-ob
- 一、基本概念Task优势ThreadPool相比Thread来说具备了很多优势,但是ThreadPool却又存在一些使用上的不方便,例如:T
- 前言写Android:如何编写“万能”的Activity的这篇文章到现在已经好久了,但是由于最近事情较多,写重构篇的计划就一直被无情的耽搁下
- 上一篇,初步开发了这个应用,功能都有了(见https://www.jb51.net/article/96992.htm 点击打开链接)。但是
- 一、问题描述在C#中is,as,using关键字具有其特点及使用场景,其中is关键字用于检查该对象是否与给定类型兼容,as关键字用于将对象转
- 算法的主题思想:1.优秀的算法因为能够解决实际问题而变得更为重要;2.高效算法的代码也可以很简单;3.理解某个实现的性能特点是一个挑战;4.
- Dubbo作为国内最出名的分布式服务框架,是Java程序员必备必会的框架之一,更是中高级测试面试过程中经常会问的技术,无论你是否用过,你都必
- 具体代码如下所示:***web.xml***<?xml version="1.0" encoding="
- 前言本篇文章 中写到的是 flutter 调用了Android 原生的 TextView 案例添加原生组件的流程基本上可以描述为:1 and
- 不依赖任何外界包,maven如何生成可以执行的jar?pom中不包含任何引用的情况下,只需要在pom中添加 maven-jar-plugin
- 一 概述GC(Garbage Collection),在程序运行过程中内存空间是有限的,为了更好的的使用有限的内存空间,GC会将不再使用的对
- Java反射机制一、什么是反射机制 简单的来
- 目录资源定义好之后,再使用时,可以指定以静态的方式使用资源,还是以动态的方式使用资源。资源我们都会使用了,接下来需要归类整理我们的资源,使用
- 抽象类1.引出抽象类向上转型带来的最大的好处就是参数统一化,使用共同的父类引用,就可以接收所有的子类实例。多态非常依赖方法覆写,但是子类可以
- 介绍: Mybatis-Plus(简称MP)