or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

collection-operations.mdcompression-operations.mddate-time-operations.mdfile-io-operations.mdindex.mdobject-operations.mdrandom-operations.mdstring-operations.mdtype-conversion.mdurl-operations.mdxml-operations.md

string-operations.mddocs/

0

# String Operations

1

2

Comprehensive string manipulation, formatting, conversion, and validation utilities through the StrUtil class.

3

4

## Capabilities

5

6

### String Validation

7

8

Check if strings are null, empty, or blank (containing only whitespace).

9

10

```java { .api }

11

/**

12

* Check if a CharSequence is blank (null, empty, or whitespace only)

13

* @param str the CharSequence to check

14

* @return true if the CharSequence is blank

15

*/

16

public static boolean isBlank(CharSequence str);

17

18

/**

19

* Check if a CharSequence is empty (null or zero length)

20

* @param str the CharSequence to check

21

* @return true if the CharSequence is empty

22

*/

23

public static boolean isEmpty(CharSequence str);

24

25

/**

26

* Check if an object is a blank string

27

* @param obj the object to check

28

* @return true if the object is a blank string

29

*/

30

public static boolean isBlankIfStr(Object obj);

31

32

/**

33

* Check if an object is an empty string

34

* @param obj the object to check

35

* @return true if the object is an empty string

36

*/

37

public static boolean isEmptyIfStr(Object obj);

38

```

39

40

**Usage Examples:**

41

42

```java

43

import cn.hutool.core.util.StrUtil;

44

45

// Blank checking

46

boolean isBlank1 = StrUtil.isBlank(null); // true

47

boolean isBlank2 = StrUtil.isBlank(""); // true

48

boolean isBlank3 = StrUtil.isBlank(" "); // true

49

boolean isBlank4 = StrUtil.isBlank("abc"); // false

50

51

// Empty checking (does not check whitespace)

52

boolean isEmpty1 = StrUtil.isEmpty(null); // true

53

boolean isEmpty2 = StrUtil.isEmpty(""); // true

54

boolean isEmpty3 = StrUtil.isEmpty(" "); // false

55

boolean isEmpty4 = StrUtil.isEmpty("abc"); // false

56

57

// Object type checking

58

boolean isBlankObj = StrUtil.isBlankIfStr(" "); // true

59

boolean isBlankObj2 = StrUtil.isBlankIfStr(123); // false

60

```

61

62

### String Formatting

63

64

Format strings using template placeholders and parameter substitution.

65

66

```java { .api }

67

/**

68

* Format string using {} placeholders

69

* @param template the template string with {} placeholders

70

* @param params the parameters to substitute

71

* @return formatted string

72

*/

73

public static String format(String template, Object... params);

74

75

/**

76

* Format string using indexed placeholders like {0}, {1}, etc.

77

* @param template the template string with indexed placeholders

78

* @param params the parameters to substitute

79

* @return formatted string

80

*/

81

public static String indexedFormat(String template, Object... params);

82

```

83

84

**Usage Examples:**

85

86

```java

87

// Basic formatting

88

String result1 = StrUtil.format("Hello {}", "World"); // "Hello World"

89

String result2 = StrUtil.format("{} + {} = {}", 1, 2, 3); // "1 + 2 = 3"

90

91

// Indexed formatting

92

String result3 = StrUtil.indexedFormat("Hello {0}, welcome to {1}", "John", "Hutool");

93

// "Hello John, welcome to Hutool"

94

```

95

96

### String Conversion

97

98

Convert objects to strings with various encoding options.

99

100

```java { .api }

101

/**

102

* Convert object to UTF-8 encoded string

103

* @param obj the object to convert

104

* @return UTF-8 encoded string representation

105

*/

106

public static String utf8Str(Object obj);

107

108

/**

109

* Convert object to string using specified charset

110

* @param obj the object to convert

111

* @param charset the charset to use

112

* @return string representation with specified encoding

113

*/

114

public static String str(Object obj, Charset charset);

115

116

/**

117

* Convert byte array to string using specified charset

118

* @param bytes the byte array to convert

119

* @param charset the charset to use

120

* @return string representation

121

*/

122

public static String str(byte[] bytes, Charset charset);

123

124

/**

125

* Convert object to string, returning null if object is null

126

* @param obj the object to convert

127

* @return string representation or null

128

*/

129

public static String toStringOrNull(Object obj);

130

131

/**

132

* Convert object to string, returning empty string if object is null

133

* @param obj the object to convert

134

* @return string representation or empty string

135

*/

136

public static String toStringOrEmpty(Object obj);

137

```

138

139

### String Trimming

140

141

Remove whitespace and perform string cleanup operations.

142

143

```java { .api }

144

/**

145

* Trim whitespace from array of strings in-place

146

* @param strs array of strings to trim

147

*/

148

public static void trim(String[] strs);

149

150

/**

151

* Remove whitespace from both ends of string

152

* @param str the string to trim

153

* @return trimmed string or null if input is null

154

*/

155

public static String trim(CharSequence str);

156

157

/**

158

* Trim to empty string if null

159

* @param str the string to trim

160

* @return trimmed string or empty string if input is null

161

*/

162

public static String trimToEmpty(CharSequence str);

163

164

/**

165

* Remove whitespace from start of string

166

* @param str the string to trim

167

* @return string with leading whitespace removed

168

*/

169

public static String trimStart(CharSequence str);

170

171

/**

172

* Remove whitespace from end of string

173

* @param str the string to trim

174

* @return string with trailing whitespace removed

175

*/

176

public static String trimEnd(CharSequence str);

177

```

178

179

### String Builders

180

181

Create StringBuilder and StrBuilder instances for efficient string building.

182

183

```java { .api }

184

/**

185

* Create a new StringBuilder

186

* @return new StringBuilder instance

187

*/

188

public static StringBuilder builder();

189

190

/**

191

* Create a new StringBuilder with specified capacity

192

* @param capacity initial capacity

193

* @return new StringBuilder instance

194

*/

195

public static StringBuilder builder(int capacity);

196

197

/**

198

* Create a new StrBuilder (Hutool's enhanced StringBuilder)

199

* @return new StrBuilder instance

200

*/

201

public static StrBuilder strBuilder();

202

203

/**

204

* Create a new StrBuilder with specified capacity

205

* @param capacity initial capacity

206

* @return new StrBuilder instance

207

*/

208

public static StrBuilder strBuilder(int capacity);

209

```

210

211

### String Readers and Writers

212

213

Create StringReader and StringWriter instances for string I/O operations.

214

215

```java { .api }

216

/**

217

* Create a StringReader for the given string

218

* @param str the string content

219

* @return StringReader instance

220

*/

221

public static StringReader getReader(CharSequence str);

222

223

/**

224

* Create a StringWriter

225

* @return StringWriter instance

226

*/

227

public static StringWriter getWriter();

228

```

229

230

**Usage Examples:**

231

232

```java

233

import cn.hutool.core.util.StrUtil;

234

import java.nio.charset.StandardCharsets;

235

236

// String conversion

237

String utf8 = StrUtil.utf8Str("Hello 世界"); // UTF-8 encoded

238

String custom = StrUtil.str("Hello".getBytes(), StandardCharsets.UTF_8);

239

240

// Safe conversions

241

String safe1 = StrUtil.toStringOrNull(null); // null

242

String safe2 = StrUtil.toStringOrEmpty(null); // ""

243

244

// String building

245

StringBuilder sb = StrUtil.builder(100);

246

sb.append("Hello").append(" ").append("World");

247

248

// String I/O

249

StringReader reader = StrUtil.getReader("Hello World");

250

StringWriter writer = StrUtil.getWriter();

251

```

252

253

## Inheritance Hierarchy

254

255

StrUtil extends CharSequenceUtil, providing access to additional string operations inherited from the parent class:

256

257

- Character sequence manipulation methods

258

- Advanced string searching and replacement

259

- String splitting and joining operations

260

- Case conversion utilities

261

262

## Constants from StrPool

263

264

StrUtil implements StrPool interface, providing access to common string constants:

265

266

```java { .api }

267

String EMPTY = ""; // Empty string

268

String NULL = "null"; // String representation of null

269

String SPACE = " "; // Single space

270

String TAB = "\t"; // Tab character

271

String DOT = "."; // Dot character

272

String SLASH = "/"; // Forward slash

273

String BACKSLASH = "\\"; // Backslash

274

```