CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-cn-hutool--hutool-core

Comprehensive Java utility library providing collections, strings, beans, dates, I/O, and numerous other utility functions.

Pending
Overview
Eval results
Files

string-operations.mddocs/

String Operations

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

Capabilities

String Validation

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

/**
 * Check if a CharSequence is blank (null, empty, or whitespace only)
 * @param str the CharSequence to check
 * @return true if the CharSequence is blank
 */
public static boolean isBlank(CharSequence str);

/**
 * Check if a CharSequence is empty (null or zero length)
 * @param str the CharSequence to check
 * @return true if the CharSequence is empty
 */
public static boolean isEmpty(CharSequence str);

/**
 * Check if an object is a blank string
 * @param obj the object to check
 * @return true if the object is a blank string
 */
public static boolean isBlankIfStr(Object obj);

/**
 * Check if an object is an empty string
 * @param obj the object to check
 * @return true if the object is an empty string
 */
public static boolean isEmptyIfStr(Object obj);

Usage Examples:

import cn.hutool.core.util.StrUtil;

// Blank checking
boolean isBlank1 = StrUtil.isBlank(null);      // true
boolean isBlank2 = StrUtil.isBlank("");        // true
boolean isBlank3 = StrUtil.isBlank("   ");     // true
boolean isBlank4 = StrUtil.isBlank("abc");     // false

// Empty checking (does not check whitespace)
boolean isEmpty1 = StrUtil.isEmpty(null);      // true
boolean isEmpty2 = StrUtil.isEmpty("");        // true
boolean isEmpty3 = StrUtil.isEmpty("   ");     // false
boolean isEmpty4 = StrUtil.isEmpty("abc");     // false

// Object type checking
boolean isBlankObj = StrUtil.isBlankIfStr("  "); // true
boolean isBlankObj2 = StrUtil.isBlankIfStr(123); // false

String Formatting

Format strings using template placeholders and parameter substitution.

/**
 * Format string using {} placeholders
 * @param template the template string with {} placeholders
 * @param params the parameters to substitute
 * @return formatted string
 */
public static String format(String template, Object... params);

/**
 * Format string using indexed placeholders like {0}, {1}, etc.
 * @param template the template string with indexed placeholders
 * @param params the parameters to substitute
 * @return formatted string
 */
public static String indexedFormat(String template, Object... params);

Usage Examples:

// Basic formatting
String result1 = StrUtil.format("Hello {}", "World"); // "Hello World"
String result2 = StrUtil.format("{} + {} = {}", 1, 2, 3); // "1 + 2 = 3"

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

String Conversion

Convert objects to strings with various encoding options.

/**
 * Convert object to UTF-8 encoded string
 * @param obj the object to convert
 * @return UTF-8 encoded string representation
 */
public static String utf8Str(Object obj);

/**
 * Convert object to string using specified charset
 * @param obj the object to convert
 * @param charset the charset to use
 * @return string representation with specified encoding
 */
public static String str(Object obj, Charset charset);

/**
 * Convert byte array to string using specified charset
 * @param bytes the byte array to convert
 * @param charset the charset to use
 * @return string representation
 */
public static String str(byte[] bytes, Charset charset);

/**
 * Convert object to string, returning null if object is null
 * @param obj the object to convert
 * @return string representation or null
 */
public static String toStringOrNull(Object obj);

/**
 * Convert object to string, returning empty string if object is null
 * @param obj the object to convert
 * @return string representation or empty string
 */
public static String toStringOrEmpty(Object obj);

String Trimming

Remove whitespace and perform string cleanup operations.

/**
 * Trim whitespace from array of strings in-place
 * @param strs array of strings to trim
 */
public static void trim(String[] strs);

/**
 * Remove whitespace from both ends of string
 * @param str the string to trim
 * @return trimmed string or null if input is null
 */
public static String trim(CharSequence str);

/**
 * Trim to empty string if null
 * @param str the string to trim
 * @return trimmed string or empty string if input is null
 */
public static String trimToEmpty(CharSequence str);

/**
 * Remove whitespace from start of string
 * @param str the string to trim
 * @return string with leading whitespace removed
 */
public static String trimStart(CharSequence str);

/**
 * Remove whitespace from end of string
 * @param str the string to trim
 * @return string with trailing whitespace removed
 */
public static String trimEnd(CharSequence str);

String Builders

Create StringBuilder and StrBuilder instances for efficient string building.

/**
 * Create a new StringBuilder
 * @return new StringBuilder instance
 */
public static StringBuilder builder();

/**
 * Create a new StringBuilder with specified capacity
 * @param capacity initial capacity
 * @return new StringBuilder instance
 */
public static StringBuilder builder(int capacity);

/**
 * Create a new StrBuilder (Hutool's enhanced StringBuilder)
 * @return new StrBuilder instance
 */
public static StrBuilder strBuilder();

/**
 * Create a new StrBuilder with specified capacity
 * @param capacity initial capacity
 * @return new StrBuilder instance
 */
public static StrBuilder strBuilder(int capacity);

String Readers and Writers

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

/**
 * Create a StringReader for the given string
 * @param str the string content
 * @return StringReader instance
 */
public static StringReader getReader(CharSequence str);

/**
 * Create a StringWriter
 * @return StringWriter instance
 */
public static StringWriter getWriter();

Usage Examples:

import cn.hutool.core.util.StrUtil;
import java.nio.charset.StandardCharsets;

// String conversion
String utf8 = StrUtil.utf8Str("Hello 世界"); // UTF-8 encoded
String custom = StrUtil.str("Hello".getBytes(), StandardCharsets.UTF_8);

// Safe conversions
String safe1 = StrUtil.toStringOrNull(null); // null
String safe2 = StrUtil.toStringOrEmpty(null); // ""

// String building
StringBuilder sb = StrUtil.builder(100);
sb.append("Hello").append(" ").append("World");

// String I/O
StringReader reader = StrUtil.getReader("Hello World");
StringWriter writer = StrUtil.getWriter();

Inheritance Hierarchy

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

  • Character sequence manipulation methods
  • Advanced string searching and replacement
  • String splitting and joining operations
  • Case conversion utilities

Constants from StrPool

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

String EMPTY = "";        // Empty string
String NULL = "null";     // String representation of null
String SPACE = " ";       // Single space
String TAB = "\t";        // Tab character
String DOT = ".";         // Dot character
String SLASH = "/";       // Forward slash
String BACKSLASH = "\\";  // Backslash

Install with Tessl CLI

npx tessl i tessl/maven-cn-hutool--hutool-core

docs

collection-operations.md

compression-operations.md

date-time-operations.md

file-io-operations.md

index.md

object-operations.md

random-operations.md

string-operations.md

type-conversion.md

url-operations.md

xml-operations.md

tile.json