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

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

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

服務器之家 - 編程語言 - Java教程 - java自定義封裝StringUtils常用工具類

java自定義封裝StringUtils常用工具類

2021-04-14 13:24Smile_Pride Java教程

這篇文章主要為大家詳細介紹了java自定義封裝StringUtils常用工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下

自定義封裝StringUtils常用工具類,供大家參考,具體內容如下

java" id="highlighter_840015">
?
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package com.demo.utils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * 字符串操作工具類
 * @author dongyangyang
 * @Date 2016/12/28 23:12
 * @Version 1.0
 *
 */
public class StringUtils {
 
 /**
  * 首字母變小寫
  * @param str
  * @return
  */
 public static String firstCharToLowerCase(String str) {
  char firstChar = str.charAt(0);
  if (firstChar >= 'A' && firstChar <= 'Z') {
   char[] arr = str.toCharArray();
   arr[0] += ('a' - 'A');
   return new String(arr);
  }
  return str;
 }
 
 /**
  * 首字母變大寫
  * @param str
  * @return
  */
 public static String firstCharToUpperCase(String str) {
  char firstChar = str.charAt(0);
  if (firstChar >= 'a' && firstChar <= 'z') {
   char[] arr = str.toCharArray();
   arr[0] -= ('a' - 'A');
   return new String(arr);
  }
  return str;
 }
 
 /**
  * 判斷是否為空
  * @param str
  * @return
  */
 public static boolean isEmpty(final String str) {
  return (str == null) || (str.length() == 0);
 }
 
 /**
  * 判斷是否不為空
  * @param str
  * @return
  */
 public static boolean isNotEmpty(final String str) {
  return !isEmpty(str);
 }
 
 /**
  * 判斷是否空白
  * @param str
  * @return
  */
 public static boolean isBlank(final String str) {
  int strLen;
  if ((str == null) || ((strLen = str.length()) == 0))
   return true;
  for (int i = 0; i < strLen; i++) {
   if (!Character.isWhitespace(str.charAt(i))) {
    return false;
   }
  }
  return true;
 }
 
 /**
  * 判斷是否不是空白
  * @param str
  * @return
  */
 public static boolean isNotBlank(final String str) {
  return !isBlank(str);
 }
 
 /**
  * 判斷多個字符串全部是否為空
  * @param strings
  * @return
  */
 public static boolean isAllEmpty(String... strings) {
  if (strings == null) {
   return true;
  }
  for (String str : strings) {
   if (isNotEmpty(str)) {
    return false;
   }
  }
  return true;
 }
 
 /**
  * 判斷多個字符串其中任意一個是否為空
  * @param strings
  * @return
  */
 public static boolean isHasEmpty(String... strings) {
  if (strings == null) {
   return true;
  }
  for (String str : strings) {
   if (isEmpty(str)) {
    return true;
   }
  }
  return false;
 }
 
 /**
  * checkValue為 null 或者為 "" 時返回 defaultValue
  * @param checkValue
  * @param defaultValue
  * @return
  */
 public static String isEmpty(String checkValue, String defaultValue) {
  return isEmpty(checkValue) ? defaultValue : checkValue;
 }
 
 /**
  * 字符串不為 null 而且不為 "" 并且等于other
  * @param str
  * @param other
  * @return
  */
 public static boolean isNotEmptyAndEquelsOther(String str, String other) {
  if (isEmpty(str)) {
   return false;
  }
  return str.equals(other);
 }
 
 /**
  * 字符串不為 null 而且不為 "" 并且不等于other
  * @param str
  * @param other
  * @return
  */
 public static boolean isNotEmptyAndNotEquelsOther(String str, String... other) {
  if (isEmpty(str)) {
   return false;
  }
  for (int i = 0; i < other.length; i++) {
   if (str.equals(other[i])) {
    return false;
   }
  }
  return true;
 }
 
 /**
  * 字符串不等于other
  * @param str
  * @param other
  * @return
  */
 public static boolean isNotEquelsOther(String str, String... other) {
  for (int i = 0; i < other.length; i++) {
   if (other[i].equals(str)) {
    return false;
   }
  }
  return true;
 }
 
 /**
  * 判斷字符串不為空
  * @param strings
  * @return
  */
 public static boolean isNotEmpty(String... strings) {
  if (strings == null) {
   return false;
  }
  for (String str : strings) {
   if (str == null || "".equals(str.trim())) {
    return false;
   }
  }
  return true;
 }
 
 /**
  * 比較字符相等
  * @param value
  * @param equals
  * @return
  */
 public static boolean equals(String value, String equals) {
  if (isAllEmpty(value, equals)) {
   return true;
  }
  return value.equals(equals);
 }
 
 /**
  * 比較字符串不相等
  * @param value
  * @param equals
  * @return
  */
 public static boolean isNotEquals(String value, String equals) {
  return !equals(value, equals);
 }
 
 public static String[] split(String content, String separatorChars) {
  return splitWorker(content, separatorChars, -1, false);
 }
 
 public static String[] split(String str, String separatorChars, int max) {
  return splitWorker(str, separatorChars, max, false);
 }
 
 public static final String[] EMPTY_STRING_ARRAY = new String[0];
 
 private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) {
  if (str == null) {
   return null;
  }
  int len = str.length();
  if (len == 0) {
   return EMPTY_STRING_ARRAY;
  }
  List<String> list = new ArrayList<String>();
  int sizePlus1 = 1;
  int i = 0, start = 0;
  boolean match = false;
  boolean lastMatch = false;
  if (separatorChars == null) {
   while (i < len) {
    if (Character.isWhitespace(str.charAt(i))) {
     if (match || preserveAllTokens) {
      lastMatch = true;
      if (sizePlus1++ == max) {
       i = len;
       lastMatch = false;
      }
      list.add(str.substring(start, i));
      match = false;
     }
     start = ++i;
     continue;
    }
    lastMatch = false;
    match = true;
    i++;
   }
  } else if (separatorChars.length() == 1) {
   char sep = separatorChars.charAt(0);
   while (i < len) {
    if (str.charAt(i) == sep) {
     if (match || preserveAllTokens) {
      lastMatch = true;
      if (sizePlus1++ == max) {
       i = len;
       lastMatch = false;
      }
      list.add(str.substring(start, i));
      match = false;
     }
     start = ++i;
     continue;
    }
    lastMatch = false;
    match = true;
    i++;
   }
  } else {
   while (i < len) {
    if (separatorChars.indexOf(str.charAt(i)) >= 0) {
     if (match || preserveAllTokens) {
      lastMatch = true;
      if (sizePlus1++ == max) {
       i = len;
       lastMatch = false;
      }
      list.add(str.substring(start, i));
      match = false;
     }
     start = ++i;
     continue;
    }
    lastMatch = false;
    match = true;
    i++;
   }
  }
  if (match || (preserveAllTokens && lastMatch)) {
   list.add(str.substring(start, i));
  }
  return (String[]) list.toArray(EMPTY_STRING_ARRAY);
 }
 
 /**
  * 消除轉義字符
  * @param str
  * @return
  */
 public static String escapeXML(String str) {
  if (str == null)
   return "";
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < str.length(); ++i) {
   char c = str.charAt(i);
   switch (c) {
   case '\u00FF':
   case '\u0024':
    break;
   case '&':
    sb.append("&amp;");
    break;
   case '<':
    sb.append("&lt;");
    break;
   case '>':
    sb.append("&gt;");
    break;
   case '\"':
    sb.append("&quot;");
    break;
   case '\'':
    sb.append("&apos;");
    break;
   default:
    if (c >= '\u0000' && c <= '\u001F')
     break;
    if (c >= '\uE000' && c <= '\uF8FF')
     break;
    if (c >= '\uFFF0' && c <= '\uFFFF')
     break;
    sb.append(c);
    break;
   }
  }
  return sb.toString();
 }
 
 /**
  * 將字符串中特定模式的字符轉換成map中對應的值
  *
  * @param s
  *   需要轉換的字符串
  * @param map
  *   轉換所需的鍵值對集合
  * @return 轉換后的字符串
  */
 public static String replace(String s, Map<String, Object> map) {
  StringBuilder ret = new StringBuilder((int) (s.length() * 1.5));
  int cursor = 0;
  for (int start, end; (start = s.indexOf("${", cursor)) != -1 && (end = s.indexOf("}", start)) != -1;) {
   ret.append(s.substring(cursor, start)).append(map.get(s.substring(start + 2, end)));
   cursor = end + 1;
  }
  ret.append(s.substring(cursor, s.length()));
  return ret.toString();
 }
 
 public static String replace(String s, Object... objs) {
  if (objs == null || objs.length == 0)
   return s;
  if (s.indexOf("{}") == -1)
   return s;
  StringBuilder ret = new StringBuilder((int) (s.length() * 1.5));
  int cursor = 0;
  int index = 0;
  for (int start; (start = s.indexOf("{}", cursor)) != -1;) {
   ret.append(s.substring(cursor, start));
   if (index < objs.length)
    ret.append(objs[index]);
   else
    ret.append("{}");
   cursor = start + 2;
   index++;
  }
  ret.append(s.substring(cursor, s.length()));
  return ret.toString();
 }
 
 /**
  * 字符串格式化工具,參數必須以{0}之類的樣式標示出來.大括號中的數字從0開始。
  *
  * @param source
  *   源字符串
  * @param params
  *   需要替換的參數列表,寫入時會調用每個參數的toString().
  * @return 替換完成的字符串。如果原始字符串為空或者參數為空那么將直接返回原始字符串。
  */
 public static String replaceArgs(String source, Object... params) {
  if (params == null || params.length == 0 || source == null || source.isEmpty()) {
   return source;
  }
  StringBuilder buff = new StringBuilder(source);
  StringBuilder temp = new StringBuilder();
  int startIndex = 0;
  int endIndex = 0;
  String param = null;
  for (int count = 0; count < params.length; count++) {
   if (params[count] == null) {
    param = null;
   } else {
    param = params[count].toString();
   }
 
   temp.delete(0, temp.length());
   temp.append("{");
   temp.append(count);
   temp.append("}");
   while (true) {
    startIndex = buff.indexOf(temp.toString(), endIndex);
    if (startIndex == -1) {
     break;
    }
    endIndex = startIndex + temp.length();
 
    buff.replace(startIndex, endIndex, param == null ? "" : param);
   }
   startIndex = 0;
   endIndex = 0;
  }
  return buff.toString();
 }
 
 public static String substringBefore(final String s, final String separator) {
  if (isEmpty(s) || separator == null) {
   return s;
  }
  if (separator.isEmpty()) {
   return "";
  }
  final int pos = s.indexOf(separator);
  if (pos < 0) {
   return s;
  }
  return s.substring(0, pos);
 }
 
 public static String substringBetween(final String str, final String open, final String close) {
  if (str == null || open == null || close == null) {
   return null;
  }
  final int start = str.indexOf(open);
  if (start != -1) {
   final int end = str.indexOf(close, start + open.length());
   if (end != -1) {
    return str.substring(start + open.length(), end);
   }
  }
  return null;
 }
 
 public static String substringAfter(final String str, final String separator) {
  if (isEmpty(str)) {
   return str;
  }
  if (separator == null) {
   return "";
  }
  final int pos = str.indexOf(separator);
  if (pos == -1) {
   return "";
  }
  return str.substring(pos + separator.length());
 }
 
 /**
 * 轉換為字節數組
 * @param str
 * @return
 */
 public static String toString(byte[] bytes){
  try {
   return new String(bytes, "utf-8");
  } catch (UnsupportedEncodingException e) {
   return null;
  }
 }
 
 /**
 * 轉換為字節數組
 * @param str
 * @return
 */
 public static byte[] getBytes(String str){
  if (str != null){
  try {
   return str.getBytes("utf-8");
   } catch (UnsupportedEncodingException e) {
   return null;
   }
  }else{
  return null;
  }
 }
 public static void main(String[] a){
  String escapeXML = escapeXML("\\");
  System.out.println(escapeXML);
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_35712358/article/details/70254546

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 无耻之徒第十一季在线观看 | bbc japanese黑人强行| 国产成人精品一区二三区在线观看 | 亚洲天堂一区二区在线观看 | 国产高清不卡视频在线播放 | 天堂激情网 | 2020最新韩国理论三级0k | 免费精品视频在线 | 欧美一级特黄特色大片免费 | 香蕉精品高清在线观看视频 | 5278欧美一区二区三区 | 金牛网155755水心论坛黄大父母 | 亚洲国产区中文在线观看 | 国产精品第1页在线播放 | 2021海角社区最新版 | 果冻传媒第一二三专区 | 国语刺激对白勾搭视频在线观看 | 欧美一区二区三区免费观看视频 | 欧美日韩高清一区 | 精品视频在线免费 | 日本hd18| 欧美人成绝费网站色www吃脚 | 午夜五月天 | 国产三级精品久久三级国专区 | 第一次做m被调教经历 | xxxxx性bbbbb欧美 | 亚洲黄色片免费看 | 欧美日韩国产一区二区三区不卡 | 国产成人精品一区二三区 | 11 13加污女qq看他下面 | 99在线精品免费视频九九视 | a毛片在线免费观看 | 国产经典一区二区三区蜜芽 | 激情亚洲天堂 | 九九热视频 这里有精品 | 国产高清免费在线 | videosxxxx老女人 | 果冻传媒91 | 嫩草香味| 91制片厂果冻传媒杨柳作品 | 五月天国产视频 |