-
Notifications
You must be signed in to change notification settings - Fork 243
/
StringUtil.java
82 lines (65 loc) · 1.74 KB
/
StringUtil.java
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
package util;
import java.util.Random;
/**
* Util for String
*
* @author liu yuning
*
*/
public class StringUtil {
private static final int DEFAULT_STRING_LENGTH = 10;
private static final String LETTERS = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static Random random = new Random();
/**
* Don't let anyone instantiate this class.
*/
private StringUtil() {
}
/**
*
* @param str
* @return {@code true} if the input {@code String} is null or ""
*/
public static boolean empty(final String str) {
return str == null || str.trim().isEmpty();
}
/**
* generate random string
*
* @return a random string
*/
public static String generateRandomString() {
return generateRandomString(DEFAULT_STRING_LENGTH);
}
/**
* generate random string using all letters and the given length
*
* @param stringLength
* @return a random string
*/
public static String generateRandomString(int stringLength) {
if (stringLength <= 0) {
stringLength = DEFAULT_STRING_LENGTH;
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < stringLength; i++) {
stringBuilder
.append(LETTERS.charAt(random.nextInt(LETTERS.length())));
}
return stringBuilder.toString();
}
/**
* repeat a {@code String} called repeatStr for repeatNum times
*
* @param repeatStr
* @param repeatNum
* @return {@code String} repeatedString
*/
public static String repeatableString(String repeatStr, int repeatNum) {
StringBuilder stringBuilder = new StringBuilder();
while (repeatNum-- > 0) {
stringBuilder.append(repeatStr);
}
return stringBuilder.toString();
}
}