0
# Core String Operations
1
2
Comprehensive string manipulation utilities through the `StrUtil` class, which extends `CharSequenceUtil` and provides null-safe string operations.
3
4
## Import
5
6
```java
7
import cn.hutool.core.util.StrUtil;
8
```
9
10
## String Validation
11
12
### Blank and Empty Checks
13
14
```java { .api }
15
// Check if string is blank (null, empty, or whitespace only)
16
public static boolean isBlank(CharSequence str);
17
public static boolean isNotBlank(CharSequence str);
18
19
// Check if string is empty (null or zero length)
20
public static boolean isEmpty(CharSequence str);
21
public static boolean isNotEmpty(CharSequence str);
22
23
// Check if object is blank string
24
public static boolean isBlankIfStr(Object obj);
25
public static boolean isEmptyIfStr(Object obj);
26
27
// Check if all strings are blank
28
public static boolean isAllBlank(CharSequence... strs);
29
public static boolean isAllEmpty(CharSequence... strs);
30
31
// Check if any string is blank
32
public static boolean hasBlank(CharSequence... strs);
33
public static boolean hasEmpty(CharSequence... strs);
34
```
35
36
### Content Validation
37
38
```java { .api }
39
// Check if string contains only letters
40
public static boolean isAlpha(CharSequence str);
41
42
// Check if string contains only digits
43
public static boolean isNumeric(CharSequence str);
44
45
// Check if string contains only letters and digits
46
public static boolean isAlphanumeric(CharSequence str);
47
48
// Check if string contains only lowercase letters
49
public static boolean isLowerCase(CharSequence str);
50
51
// Check if string contains only uppercase letters
52
public static boolean isUpperCase(CharSequence str);
53
```
54
55
## String Formatting and Templates
56
57
### Template Formatting
58
59
```java { .api }
60
// Format string with positional parameters
61
public static String format(String template, Object... params);
62
63
// Format string with named parameters
64
public static String format(String template, Map<String, Object> map);
65
66
// Format string with custom placeholder
67
public static String format(String template, Map<String, Object> map,
68
boolean ignoreNull);
69
```
70
71
**Usage Examples:**
72
73
```java
74
// Positional parameters
75
String result = StrUtil.format("Hello {}, welcome to {}", "World", "Hutool");
76
// Output: "Hello World, welcome to Hutool"
77
78
// Named parameters
79
Map<String, Object> params = MapUtil.of("name", "John", "age", 25);
80
String result = StrUtil.format("Name: {name}, Age: {age}", params);
81
// Output: "Name: John, Age: 25"
82
83
// With null handling
84
String result = StrUtil.format("Value: {missing}",
85
MapUtil.of("other", "value"), true);
86
// Output: "Value: {missing}" (placeholder preserved for missing values)
87
```
88
89
## String Manipulation
90
91
### Case Conversion
92
93
```java { .api }
94
// Convert to various cases
95
public static String toCamelCase(CharSequence str);
96
public static String toUnderlineCase(CharSequence str);
97
public static String upperFirst(CharSequence str);
98
public static String lowerFirst(CharSequence str);
99
public static String swapCase(CharSequence str);
100
```
101
102
### Trimming and Padding
103
104
```java { .api }
105
// Trim operations
106
public static String trim(CharSequence str);
107
public static String trimStart(CharSequence str);
108
public static String trimEnd(CharSequence str);
109
public static void trim(String[] strs);
110
111
// Padding operations
112
public static String padPre(CharSequence str, int minLength, char padChar);
113
public static String padAfter(CharSequence str, int minLength, char padChar);
114
public static String center(CharSequence str, int size, char padChar);
115
```
116
117
### Substring Operations
118
119
```java { .api }
120
// Safe substring operations
121
public static String sub(CharSequence str, int fromIndex, int toIndex);
122
public static String subPre(CharSequence str, int toIndex);
123
public static String subSuf(CharSequence str, int fromIndex);
124
125
// Substring with specific delimiters
126
public static String subBefore(CharSequence str, CharSequence separator,
127
boolean isLastSeparator);
128
public static String subAfter(CharSequence str, CharSequence separator,
129
boolean isLastSeparator);
130
public static String subBetween(CharSequence str, CharSequence before,
131
CharSequence after);
132
```
133
134
## String Joining and Splitting
135
136
### Joining Operations
137
138
```java { .api }
139
// Join collections with delimiter
140
public static String join(CharSequence delimiter, Object... elements);
141
public static String join(CharSequence delimiter, Iterable<?> iterable);
142
143
// Join arrays
144
public static <T> String join(CharSequence delimiter, T[] array);
145
public static String join(CharSequence delimiter, Object[] array,
146
int startIndex, int endIndex);
147
```
148
149
### Splitting Operations
150
151
```java { .api }
152
// Split string by delimiter
153
public static String[] split(CharSequence str, CharSequence separator);
154
public static String[] split(CharSequence str, char separator);
155
public static List<String> splitTrim(CharSequence str, CharSequence separator);
156
157
// Split with limit
158
public static String[] split(CharSequence str, CharSequence separator, int limit);
159
160
// Split by regex
161
public static String[] splitByRegex(CharSequence str, String regex);
162
```
163
164
## String Searching and Replacement
165
166
### Search Operations
167
168
```java { .api }
169
// Check if string contains substring
170
public static boolean contains(CharSequence str, CharSequence searchStr);
171
public static boolean containsIgnoreCase(CharSequence str, CharSequence searchStr);
172
173
// Count occurrences
174
public static int count(CharSequence content, CharSequence strForSearch);
175
176
// Index operations
177
public static int indexOf(CharSequence str, CharSequence searchStr,
178
int fromIndex, boolean ignoreCase);
179
public static int lastIndexOf(CharSequence str, CharSequence searchStr,
180
int fromIndex, boolean ignoreCase);
181
```
182
183
### Replace Operations
184
185
```java { .api }
186
// Replace operations
187
public static String replace(CharSequence str, CharSequence searchStr,
188
CharSequence replacement);
189
public static String replaceIgnoreCase(CharSequence str, CharSequence searchStr,
190
CharSequence replacement);
191
192
// Replace first/last occurrence
193
public static String replaceFirst(CharSequence str, CharSequence searchStr,
194
CharSequence replacement);
195
public static String replaceLast(CharSequence str, CharSequence searchStr,
196
CharSequence replacement);
197
198
// Replace by regex
199
public static String replaceByRegex(CharSequence str, String regex,
200
String replacement);
201
```
202
203
## String Comparison
204
205
### Equality and Similarity
206
207
```java { .api }
208
// Equality checks
209
public static boolean equals(CharSequence str1, CharSequence str2);
210
public static boolean equalsIgnoreCase(CharSequence str1, CharSequence str2);
211
212
// Prefix and suffix checks
213
public static boolean startWith(CharSequence str, CharSequence prefix);
214
public static boolean startWithIgnoreCase(CharSequence str, CharSequence prefix);
215
public static boolean endWith(CharSequence str, CharSequence suffix);
216
public static boolean endWithIgnoreCase(CharSequence str, CharSequence suffix);
217
218
// String similarity
219
public static double similar(CharSequence str1, CharSequence str2);
220
```
221
222
## Utility Methods
223
224
### String Generators
225
226
```java { .api }
227
// Repeat string
228
public static String repeat(CharSequence str, int count);
229
public static String repeat(char ch, int count);
230
231
// Generate random string
232
public static String random(int count);
233
public static String random(int count, String baseString);
234
```
235
236
### String Conversion
237
238
```java { .api }
239
// Byte conversion
240
public static String str(byte[] bytes, Charset charset);
241
public static String utf8Str(byte[] bytes);
242
243
// Object to string
244
public static String toString(Object obj);
245
public static String toString(Object obj, String nullDefault);
246
247
// Wrap operations
248
public static String wrap(CharSequence str, CharSequence prefixAndSuffix);
249
public static String wrap(CharSequence str, CharSequence prefix, CharSequence suffix);
250
public static String unwrap(CharSequence str, CharSequence prefixAndSuffix);
251
```
252
253
## Constants and Pools
254
255
### String Constants
256
257
The `StrUtil` class implements `StrPool` interface providing common string constants:
258
259
```java { .api }
260
// Common characters
261
String SPACE = " ";
262
String TAB = "\t";
263
String DOT = ".";
264
String SLASH = "/";
265
String BACKSLASH = "\\";
266
String CR = "\r";
267
String LF = "\n";
268
String CRLF = "\r\n";
269
String UNDERLINE = "_";
270
String DASHED = "-";
271
String COMMA = ",";
272
273
// Common strings
274
String NULL = "null";
275
String EMPTY = "";
276
String TRUE = "true";
277
String FALSE = "false";
278
279
// HTML entities
280
String AMP = "&";
281
String QUOTE = """;
282
String APOS = "'";
283
String LT = "<";
284
String GT = ">";
285
```
286
287
All string operations in Hutool are null-safe and follow consistent naming conventions. Methods return `null` for `null` inputs unless documented otherwise, and support both `String` and `CharSequence` parameters for maximum flexibility.