Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:APIJSON APIJSON StringUtil

From Leeroopedia
Knowledge Sources
Domains String_Processing, Validation, Request_Processing
Last Updated 2026-10-11 00:00 GMT

Overview

Concrete utility class providing null-safe string operations, format validation, type checking, splitting, and case conversion for the entire APIJSON framework.

Description

The StringUtil class is the foundational string manipulation utility in APIJSON. Nearly every other class in the framework depends on it for null-safe string handling — all get methods return empty string ("") instead of null, eliminating NullPointerException risk throughout the codebase.

The class provides these functional groups:

  • Null-safe accessors: get(Object/String/Object[]) — converts null to "", joins arrays with a delimiter
  • Whitespace handling: trim (leading/trailing), noBlank (all whitespace removed)
  • Emptiness checks: isEmpty / isNotEmpty with optional trim — also side-effects a thread-local current field for chained operations
  • Length: length with optional trim
  • Type/format validation: isNumber, isAlpha, isNumberOrAlpha, isCombineOfNumOrAlpha, isName, isBigName, isSmallName, isPhone, isEmail, isIDCard, isUrl, isBranchUrl, isFilePath, isPath, isPassword, isNumberPassword, isVerify
  • String extraction: getNumber — extracts numeric characters, optionally only from the start
  • String correction: getCorrectUrl (auto-prefix http://), getCorrectPhone (strip +86, spaces, dashes), getCorrectEmail (auto-suffix .com)
  • Price formatting: getPrice — formats to two decimal places with optional currency prefix/suffix (¥, 元)
  • Splitting/joining: split (multi-delimiter with optional trim), join (array join with delimiter), concat (two-string join), splitPath (path segmentation)
  • Case conversion: firstCase (lowercase/uppercase first char), toUpperCase, toLowerCase, addSuffix (concatenate with first-letter casing)
  • Comparison: equals, equalsIgnoreCase
  • Predefined regex patterns: PATTERN_NUMBER, PATTERN_PHONE, PATTERN_EMAIL, PATTERN_ID_CARD, PATTERN_ALPHA, PATTERN_NAME, etc.

⚠️ Deprecation Notice: This class contains 17 @Deprecated legacy alias methods (getCurrentString, getString, getTrimedString, getNoBlankString, getLength) that are trivial delegates to the canonical short-name methods (cur, get, trim, noBlank, length). Each has a FIXME comment in the source instructing callers to use the replacement. Do not call the deprecated variants in new code — see Heuristic:APIJSON_APIJSON_Warning_Deprecated_StringUtil_Legacy_Aliases for the full mapping.

The isName / isBigName / isSmallName methods are particularly significant: they validate that a string is a legal code identifier (letter/underscore first, then alphanumeric/underscore/dot/colon), which is used for table-key validation throughout the request DSL.

Usage

  • Import this class whenever you need null-safe string handling in APIJSON — it is the standard way to convert objects to strings without null checks
  • Use isName / isBigName when validating that a key is a legal APIJSON table or column identifier
  • Use isNumber / isEmail / isPhone when validating user input format before database operations
  • Use split / join / concat for parsing comma-separated configuration strings (e.g., MUST and REFUSE field lists)

Code Reference

Source Location

Signature

public class StringUtil {

    // Constants
    public static final String UTF_8 = "utf-8";
    public static final String EMPTY = "无";
    public static final String UNKNOWN = "未知";

    // Predefined regex patterns
    public static final Pattern PATTERN_NUMBER;
    public static final Pattern PATTERN_PHONE;
    public static final Pattern PATTERN_EMAIL;
    public static final Pattern PATTERN_ID_CARD;
    public static final Pattern PATTERN_NUM_OR_ALPHA;
    public static final Pattern PATTERN_ALPHA;
    public static final Pattern PATTERN_NAME;
    public static final Pattern PATTERN_ALPHA_BIG;
    public static final Pattern PATTERN_ALPHA_SMALL;
    public static final Pattern PATTERN_BRANCH_URL;

    // Null-safe accessors
    public static String get(Object obj);
    public static String get(String s);
    public static String get(Object[] arr);
    public static String get(Object[] arr, boolean ignoreEmptyItem);
    public static String get(Object[] arr, String split);
    public static String get(Object[] arr, String split, boolean ignoreEmptyItem);

    // Whitespace handling
    public static String trim(Object obj);
    public static String trim(CharSequence cs);
    public static String trim(String s);
    public static String noBlank(Object obj);
    public static String noBlank(CharSequence cs);
    public static String noBlank(String s);

    // Emptiness checks
    public static boolean isEmpty(Object obj);
    public static boolean isEmpty(Object obj, boolean trim);
    public static boolean isEmpty(CharSequence cs);
    public static boolean isEmpty(CharSequence cs, boolean trim);
    public static boolean isEmpty(String s);
    public static boolean isEmpty(String s, boolean trim);
    public static boolean isNotEmpty(Object obj);
    public static boolean isNotEmpty(Object obj, boolean trim);
    public static boolean isNotEmpty(CharSequence cs);
    public static boolean isNotEmpty(CharSequence cs, boolean trim);
    public static boolean isNotEmpty(String s);
    public static boolean isNotEmpty(String s, boolean trim);

    // Length
    public static int length(Object object, boolean trim);
    public static int length(CharSequence cs, boolean trim);
    public static int length(String s, boolean trim);

    // Type/format validation
    public static boolean isNumber(String s);
    public static boolean isAlpha(String s);
    public static boolean isNumberOrAlpha(String s);
    public static boolean isCombineOfNumOrAlpha(String s);
    public static boolean isName(String s);
    public static boolean isBigName(String s);
    public static boolean isSmallName(String s);
    public static boolean isPhone(String phone);
    public static boolean isPassword(String s);
    public static boolean isNumberPassword(String s);
    public static boolean isEmail(String email);
    public static boolean isVerify(String s);
    public static boolean isIDCard(String number);
    public static boolean isUrl(String url);
    public static boolean isBranchUrl(String branchUrl);
    public static boolean isFilePath(String path);
    public static boolean isPath(String path);
    public static boolean isFilePathExist(String path);

    // String extraction
    public static String getNumber(Object object);
    public static String getNumber(CharSequence cs);
    public static String getNumber(String s);
    public static String getNumber(String s, boolean onlyStart);

    // String correction
    public static String getCorrectUrl(String url);
    public static String getCorrectPhone(String phone);
    public static String getCorrectEmail(String email);

    // Price formatting
    public static String getPrice(String price);
    public static String getPrice(String price, int formatType);
    public static String getPrice(BigDecimal price);
    public static String getPrice(double price);
    public static String getPrice(BigDecimal price, int formatType);
    public static String getPrice(double price, int formatType);

    // Splitting / joining / concat
    public static String[] split(String s);
    public static String[] split(String s, String split);
    public static String[] split(String s, boolean trim);
    public static String[] split(String s, String split, boolean trim);
    public static String[] splitPath(String path);
    public static String join(String[] arr, String s);
    public static String concat(String left, String right);
    public static String concat(String left, String right, String split);
    public static String concat(String left, String right, boolean trim);
    public static String concat(String left, String right, String split, boolean trim);

    // Case conversion
    public static String firstCase(String key);
    public static String firstCase(String key, boolean upper);
    public static String addSuffix(String key, String suffix);
    public static String toUpperCase(String s);
    public static String toUpperCase(String s, boolean trim);
    public static String toLowerCase(String s);
    public static String toLowerCase(String s, boolean trim);

    // Comparison
    public static boolean equals(Object s1, Object s2);
    public static boolean equalsIgnoreCase(String s1, String s2);

    // Thread-local current value
    public static String cur();
}

Import

import apijson.StringUtil;

I/O Contract

Inputs

Name Type Required Description
obj/s/cs Object/String/CharSequence Yes Value to convert, trim, check, or transform
arr Object[] No Array to join into a delimited string
split String No Delimiter for join/split (default ,)
ignoreEmptyItem boolean No Skip empty items when joining array (default false)
trim boolean No Whether to trim before checking/processing (default true for isEmpty)
formatType int No Price format style (0=default, 1=prefix ¥, 2=suffix 元, etc.)
onlyStart boolean No For getNumber: stop at first non-digit (default false)
upper boolean No For firstCase: uppercase first letter (default false)

Outputs

Name Type Description
get(...) String Always non-null (null becomes "'")
trim(...) / noBlank(...) String Trimmed/whitespace-removed string
isEmpty(...) / isNotEmpty(...) boolean Emptiness check result
isNumber/isEmail/isPhone/... boolean Format validation result
isName/isBigName/isSmallName boolean Identifier validity check
split(...) String[] Array of segments (null if input empty)
getPrice(...) String Formatted price with 2 decimals
cur() String Last value processed by isEmpty (thread-local)

Usage Examples

Null-Safe String Handling

import apijson.StringUtil;

// get() never returns null — converts null to ""
String s1 = StringUtil.get((Object) null);      // ""
String s2 = StringUtil.get("hello");              // "hello"
String s3 = StringUtil.get(123);                  // "123"

// Trim and noBlank
String trimmed = StringUtil.trim("  hello  ");    // "hello"
String noBlank = StringUtil.noBlank(" a b c ");   // "abc"

// Emptiness check (with optional trim)
boolean empty = StringUtil.isEmpty("   ", true);  // true (trimmed to "")
boolean notEmpty = StringUtil.isNotEmpty("x");    // true

Validating Keys and Input Formats

import apijson.StringUtil;

// Identifier validation — used for table/column key checking
boolean valid = StringUtil.isName("userId");       // true
boolean big = StringUtil.isBigName("Moment");        // true (first char uppercase)
boolean small = StringUtil.isSmallName("momentId");  // true (first char lowercase)

// Format validation
StringUtil.isNumber("12345");        // true
StringUtil.isEmail("a@b.com");        // true
StringUtil.isPhone("13812345678");   // true
StringUtil.isUrl("https://apijson.cn"); // true
StringUtil.isIDCard("110101199003073910"); // true (18-digit)

Splitting Configuration Strings

import apijson.StringUtil;

// Split comma-separated MUST/REFUSE field lists
String[] fields = StringUtil.split("id,name,momentId", ",");
// → ["id", "name", "momentId"]

// Split with trim of leading/trailing delimiters
String[] trimmed = StringUtil.split(",id,name,", ",", true);
// → ["id", "name"]

// Join array into string
String joined = StringUtil.join(new String[]{"a", "b", "c"}, "-");
// → "a-b-c"

// Concat two strings with delimiter
String result = StringUtil.concat("WHERE", "id = 1", " ");
// → "WHERE id = 1"

Price Formatting

import apijson.StringUtil;

// Format price to 2 decimal places
String p1 = StringUtil.getPrice("99.5");                    // "99.50"
String p2 = StringUtil.getPrice("99.5", StringUtil.PRICE_FORMAT_PREFIX);    // "¥99.50"
String p3 = StringUtil.getPrice("99.5", StringUtil.PRICE_FORMAT_SUFFIX);     // "99.50元"
String p4 = StringUtil.getPrice(1234.567);                   // "1234.57"

Related Pages

Implements Principle

Uses Heuristic

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment