0
# String Processing
1
2
Comprehensive string processing utilities including validation, transformation, formatting, and pattern matching. These utilities provide null-safe operations and are designed for high-performance text processing in microservices environments.
3
4
## Capabilities
5
6
### Core String Utilities
7
8
The StringUtils class provides the main string manipulation functionality with 50+ methods for common string operations.
9
10
```java { .api }
11
/**
12
* Comprehensive string manipulation utilities extracted from Apache Commons Lang
13
*/
14
public class StringUtils {
15
public static final int INDEX_NOT_FOUND = -1;
16
public static final String EMPTY = "";
17
public static final String SPACE = " ";
18
public static final String EMAIL_PATTERN = "..."; // Email validation regex
19
public static final Pattern pattern; // Compiled email pattern
20
21
// Basic checks
22
public static boolean isNullOrEmpty(String value);
23
public static boolean isEmpty(CharSequence cs);
24
public static boolean isBlank(CharSequence cs);
25
public static boolean isNotBlank(CharSequence cs);
26
public static boolean hasText(String str);
27
public static boolean hasLength(String str);
28
29
// String transformation
30
public static String expandEnvVars(String text);
31
public static String replace(String text, String searchString, String replacement, int max);
32
public static String replaceOnce(String text, String searchString, String replacement);
33
public static String rightPad(String str, int size, char padChar);
34
public static String rightPad(String str, int size, String padStr);
35
public static String repeat(char ch, int repeat);
36
public static String trimToEmpty(String str);
37
public static String removeEnd(String str, String remove);
38
public static String removeStart(String str, String remove);
39
public static String removeStartIgnoreCase(String str, String remove);
40
public static String stripEnd(String str, String stripChars);
41
42
// Substring operations
43
public static String substringBefore(String str, String separator);
44
public static String substringAfter(String str, String separator);
45
public static String substringBeforeLast(String str, String separator);
46
public static String substringAfterLast(String str, String separator);
47
48
// String analysis
49
public static int countMatches(CharSequence str, CharSequence sub);
50
public static int countMatches(CharSequence str, char ch);
51
public static boolean startsWithIgnoreCase(CharSequence str, CharSequence prefix);
52
public static boolean containsIgnoreCase(CharSequence str, CharSequence searchStr);
53
54
// Splitting and joining
55
public static String[] split(String str, String separatorChars);
56
public static String[] split(String str, char separatorChar);
57
public static String join(Object[] array, char separator);
58
public static String join(Object[] array, char separator, int startIndex, int endIndex);
59
public static String join(Iterator<?> iterator, char separator);
60
public static String join(Iterable<?> iterable, char separator);
61
public static String[] tokenizeToStringArray(String str, String delimiters);
62
public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens);
63
public static String[] toStringArray(Collection<String> collection);
64
public static String collectionToDelimitedString(Collection<?> coll, String delim);
65
public static String collectionToDelimitedString(Collection<?> coll, String delim, String prefix, String suffix);
66
public static String collectionToCommaDelimitedString(Collection<?> coll);
67
68
// Validation
69
public static boolean isEmail(String userIdEmail);
70
public static boolean isNumeric(String str);
71
public static boolean isJwtToken(String str);
72
73
// Conversion
74
public static String inputStreamToString(InputStream inputStream, Charset charset);
75
76
// Security
77
public static String maskHalfString(String str);
78
79
// Pattern matching
80
public static boolean matchPathToPattern(String requestPath, String endpointPattern);
81
82
// Utility
83
public static String getSecondPart(String input);
84
}
85
```
86
87
**Usage Examples:**
88
89
```java
90
import com.networknt.utility.StringUtils;
91
92
// Basic validation
93
boolean isEmpty = StringUtils.isEmpty(""); // true
94
boolean isBlank = StringUtils.isBlank(" "); // true
95
boolean hasText = StringUtils.hasText("hello"); // true
96
97
// String cleaning
98
String cleaned = StringUtils.trimToEmpty(" hello world "); // "hello world"
99
String padded = StringUtils.rightPad("test", 10, '-'); // "test------"
100
101
// Environment variable expansion
102
String expanded = StringUtils.expandEnvVars("Hello ${USER}"); // "Hello john"
103
104
// Email validation
105
boolean validEmail = StringUtils.isEmail("user@example.com"); // true
106
107
// String splitting and joining
108
String[] parts = StringUtils.split("a,b,c", ","); // ["a", "b", "c"]
109
String joined = StringUtils.join(new String[]{"a", "b", "c"}, ','); // "a,b,c"
110
111
// Substring operations
112
String before = StringUtils.substringBefore("hello.world", "."); // "hello"
113
String after = StringUtils.substringAfter("hello.world", "."); // "world"
114
115
// Pattern matching for paths
116
boolean matches = StringUtils.matchPathToPattern("/api/users/123", "/api/users/{id}"); // true
117
```
118
119
### Character Sequence Utilities
120
121
Null-safe CharSequence operations for working with strings and other character sequences.
122
123
```java { .api }
124
/**
125
* CharSequence operations that are null-safe
126
*/
127
public class CharSequenceUtils {
128
public CharSequenceUtils(); // JavaBean compatibility
129
130
/**
131
* Safe subsequence extraction
132
* @param cs - CharSequence to extract from
133
* @param start - Starting index
134
* @return Subsequence or null if cs is null
135
*/
136
public static CharSequence subSequence(CharSequence cs, int start);
137
}
138
```
139
140
### Character Utilities
141
142
Character manipulation and validation utilities with ASCII checking and conversion operations.
143
144
```java { .api }
145
/**
146
* Character manipulation and validation utilities
147
*/
148
public class CharUtils {
149
public static final char LF = '\n';
150
public static final char CR = '\r';
151
public static final char NUL = '\0';
152
153
public CharUtils(); // JavaBean compatibility
154
155
// Character conversion
156
public static Character toCharacterObject(char ch);
157
public static Character toCharacterObject(String str);
158
public static char toChar(Character ch, char defaultValue);
159
public static char toChar(String str, char defaultValue);
160
public static int toIntValue(char ch);
161
public static int toIntValue(Character ch);
162
public static String toString(char ch);
163
public static String toString(Character ch);
164
public static String unicodeEscaped(char ch);
165
public static String unicodeEscaped(Character ch);
166
167
// ASCII validation
168
public static boolean isAscii(char ch);
169
public static boolean isAsciiPrintable(char ch);
170
public static boolean isAsciiControl(char ch);
171
public static boolean isAsciiAlpha(char ch);
172
public static boolean isAsciiAlphaUpper(char ch);
173
public static boolean isAsciiAlphaLower(char ch);
174
public static boolean isAsciiNumeric(char ch);
175
public static boolean isAsciiAlphanumeric(char ch);
176
177
// Character comparison
178
public static int compare(char x, char y);
179
}
180
```
181
182
**Usage Examples:**
183
184
```java
185
import com.networknt.utility.CharUtils;
186
187
// Character validation
188
boolean isAlpha = CharUtils.isAsciiAlpha('A'); // true
189
boolean isNumeric = CharUtils.isAsciiNumeric('5'); // true
190
boolean isPrintable = CharUtils.isAsciiPrintable(' '); // true
191
192
// Character conversion
193
String unicode = CharUtils.unicodeEscaped('€'); // "\\u20AC"
194
int value = CharUtils.toIntValue('5'); // 53 (ASCII value)
195
196
// Safe character extraction
197
char safeChar = CharUtils.toChar("A", 'X'); // 'A'
198
char defaultChar = CharUtils.toChar("", 'X'); // 'X'
199
```
200
201
### Regular Expression Utilities
202
203
Regular expression helpers for common string processing tasks with pattern-based operations.
204
205
```java { .api }
206
/**
207
* Regular expression helpers for string processing
208
*/
209
public class RegExUtils {
210
// Pattern-based operations
211
public static String removeAll(String text, Pattern regex);
212
public static String removeAll(String text, String regex);
213
public static String removeFirst(String text, Pattern regex);
214
public static String removeFirst(String text, String regex);
215
public static String removePattern(String text, String regex);
216
217
// Replacement operations
218
public static String replaceAll(String text, Pattern regex, String replacement);
219
public static String replaceAll(String text, String regex, String replacement);
220
public static String replaceFirst(String text, Pattern regex, String replacement);
221
public static String replaceFirst(String text, String regex, String replacement);
222
public static String replacePattern(String text, String regex, String replacement);
223
}
224
```
225
226
**Usage Examples:**
227
228
```java
229
import com.networknt.utility.RegExUtils;
230
231
// Remove operations
232
String cleaned = RegExUtils.removeAll("abc123def456", "\\d+"); // "abcdef"
233
String first = RegExUtils.removeFirst("abc123def456", "\\d+"); // "abcdef456"
234
235
// Replace operations
236
String replaced = RegExUtils.replaceAll("hello world", "\\s+", "_"); // "hello_world"
237
String firstReplaced = RegExUtils.replaceFirst("test test test", "test", "demo"); // "demo test test"
238
```
239
240
### Byte Utilities
241
242
Byte array operations and random string generation for low-level data manipulation.
243
244
```java { .api }
245
/**
246
* Byte array operations and random string generation
247
*/
248
public class ByteUtil {
249
// Byte array conversion
250
public static byte[] longToBytes(long x);
251
public static long bytesToLong(byte[] bytes);
252
253
// Random string generation
254
public static String randomAlphabetic(int length);
255
public static String randomNumeric(int length);
256
}
257
```
258
259
**Usage Examples:**
260
261
```java
262
import com.networknt.utility.ByteUtil;
263
264
// Byte array conversion
265
long value = 12345L;
266
byte[] bytes = ByteUtil.longToBytes(value);
267
long restored = ByteUtil.bytesToLong(bytes); // 12345
268
269
// Random string generation
270
String randomText = ByteUtil.randomAlphabetic(10); // "aBcDeFgHiJ"
271
String randomNumbers = ByteUtil.randomNumeric(6); // "123456"
272
```