一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java判斷字符串是否為IP地址的方法

Java判斷字符串是否為IP地址的方法

2020-08-09 22:40簡簡單單OnlineZuozuo Java教程

這篇文章主要為大家詳細介紹了Java判斷字符串是否為IP地址的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Java 判斷字符串是否為IP地址,供大家參考,具體內容如下

1、代碼

主要就是這么幾個條件

  • 非空
  • 長度符合 0.0.0.0 - 255.255.255.255
  • 包含分隔符 且 個數正確
  • 四個全部是數字,且都在合理的范圍內
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * 判斷某個字符串是否是一個 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 工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
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 工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天堂成人影院 | 小鸟酱视频在线观看 | 2023毛片| 九九在线精品亚洲国产 | 99九九成人免费视频精品 | 秋霞理论一级在线观看手机版 | 国模孕妇季玥337p人体 | 久久婷婷丁香五月色综合啪免费 | 奶大逼紧 | 1024免费永久福利视频 | 800精品国产导航 | 欧美久久久久久久一区二区三区 | 玩乳h文奶水和尚 | 久久精品中文闷骚内射 | 欧美性4khd720 | 四虎影院新地址 | 亚洲国产精品综合久久一线 | 四虎影院精品在线观看 | 成人永久免费 | 四虎免费影院4hu永久免费 | 我们中文在线观看免费完整版 | 亚洲国产精品免费在线观看 | 免费国产在线观看 | 高清国语自产拍免费视频国产 | 给我免费观看的视频在线播放 | 日韩二区三区 | 色愉拍亚洲偷自拍 | 成人综合婷婷国产精品久久免费 | 欧美精品亚洲精品日韩专区va | 国产一卡2卡3卡四卡高清 | 高清在线免费观看 | 天堂a免费视频在线观看 | 四虎精品永久在线网址 | 99香蕉网 | 成人aaaa | 亚洲免费二区 | ysl千人千色t9t9t9 | 免费在线中文字幕 | 四虎1515hh.com| 我和子伦系列小说 | 精品久久久久久综合网 |