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 (ains(regex) && str.split(regex).length == 4) {
boolean legalNumber = true;
/
/ 四个全部是数字,且都在合理的范围内
for (String obj : wArrayList(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.zjcsmon.util;
import 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) && bigDecimalpareTo(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) && bigDecimalpareTo(BigDecimal.ONE) == 0; }
/**
* 是否不是 1
*
* @param bigDecimal
* @return 不是 1 返回true
*/
public static boolean isNotOneDecimal(BigDecimal bigDecimal) {
return bigDecimalpareTo(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 ipareTo(j) == 0;
}
/**
* 第⼀个 decimal 是否⼤于第⼆个 decimal
*
* @param i
* @param j
* @return ⼤于返回true
*/
public static boolean isDecimalGt(BigDecimal i, BigDecimal j) {
return ipareTo(j) > 0;
}
/**
* 第⼀个 decimal 是否⼩于第⼆个 decimal
*
* @param i
* @param j
* @return ⼩于返回true
*/
public static boolean isDecimalLt(BigDecimal i, BigDecimal j) {
return ipareTo(j) < 0;
}
/**
* 特殊字符串处理
*
* @param character
* @return
*/
public static String replaceSpecialCharacter(String character) {
String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]";
placeAll(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 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 * Denominator() + denominator * Numerator();  long d = denominator * Denominator();
return new Fraction(n, d);
}
/**
* 分数的减法
*
*/
public Fraction subtract(Fraction secondRational) {
long n = numerator * Denominator() - denominator * Numerator();  long d = denominator * Denominator();
return new Fraction(n, d);
}
/**
* 分数乘法
*
*/
public Fraction mulitiply(Fraction secondRational) {
long n = numerator * Numerator();
long d = denominator * Denominator();
return new Fraction(n, d);
}
bigdecimal转换为integer/**
* 分数除法
*
*/
public Fraction divide(Fraction secondRational) {
long n = numerator * Denominator();
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();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。