Class Splitter

java.lang.Object
cloud.opencode.base.core.Splitter

public final class Splitter extends Object
Splitter - Fluent string splitting utility 分割器 - 流式字符串分割工具

Provides a fluent API for splitting strings with various options for trimming, omitting empty strings, and limiting results.

提供用于分割字符串的流式 API,支持修剪、省略空字符串和限制结果等选项。

Usage Examples | 使用示例:

// Basic splitting
List<String> parts = Splitter.on(',').splitToList("a,b,c");  // ["a", "b", "c"]

// Trim whitespace
List<String> trimmed = Splitter.on(',').trimResults().splitToList("a , b , c");

// Omit empty strings
List<String> nonEmpty = Splitter.on(',').omitEmptyStrings().splitToList("a,,b,c");

// Fixed length splitting
List<String> fixed = Splitter.fixedLength(3).splitToList("abcdefg");  // ["abc", "def", "g"]

// Limit results
List<String> limited = Splitter.on(',').limit(2).splitToList("a,b,c,d");  // ["a", "b,c,d"]

// Split to map
Map<String, String> map = Splitter.on(',').withKeyValueSeparator('=')
    .split("a=1,b=2,c=3");  // {a=1, b=2, c=3}

// Pattern-based splitting
List<String> regex = Splitter.onPattern("\\s+").splitToList("a  b   c");

Features | 主要功能:

  • Fluent API for string splitting - 字符串分割的流式API
  • Trim, omit empty, and limit options - 修剪、省略空串和限制选项
  • Fixed-length and regex splitting - 固定长度和正则表达式分割
  • Key-value map splitting - 键值Map分割

Security | 安全性:

  • Thread-safe: Yes (immutable after creation) - 线程安全: 是(创建后不可变)
  • Null-safe: No, input must not be null - 空值安全: 否,输入不可为null
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • OpenCode.cloud

    For an extended version with CharMatcher support, see cloud.opencode.base.string.builder.Splitter in opencode-base-string.

  • Joiner
  • Method Details

    • on

      public static Splitter on(char separator)
      Creates a splitter that uses the given character as separator. 创建使用给定字符作为分隔符的分割器。
      Parameters:
      separator - the separator character - 分隔符字符
      Returns:
      the splitter - 分割器
    • on

      public static Splitter on(String separator)
      Creates a splitter that uses the given string as separator. 创建使用给定字符串作为分隔符的分割器。
      Parameters:
      separator - the separator string - 分隔符字符串
      Returns:
      the splitter - 分割器
    • on

      public static Splitter on(Pattern pattern)
      Creates a splitter that uses the given pattern as separator. 创建使用给定模式作为分隔符的分割器。
      Parameters:
      pattern - the pattern - 模式
      Returns:
      the splitter - 分割器
    • onPattern

      public static Splitter onPattern(String regex)
      Creates a splitter that uses the given regex pattern as separator. 创建使用给定正则表达式模式作为分隔符的分割器。

      The pattern is compiled on each call. For repeated use, cache the Splitter instance or use on(Pattern) with a pre-compiled Pattern.

      每次调用都会编译模式。重复使用时请缓存 Splitter 实例, 或使用 on(Pattern) 传入预编译的 Pattern。

      Parameters:
      regex - the regex pattern - 正则表达式模式
      Returns:
      the splitter - 分割器
    • fixedLength

      public static Splitter fixedLength(int length)
      Creates a splitter that splits strings into fixed-length parts. 创建将字符串分割为固定长度部分的分割器。
      Parameters:
      length - the fixed length - 固定长度
      Returns:
      the splitter - 分割器
    • omitEmptyStrings

      public Splitter omitEmptyStrings()
      Returns a splitter that omits empty strings from results. 返回从结果中省略空字符串的分割器。
      Returns:
      the splitter - 分割器
    • trimResults

      public Splitter trimResults()
      Returns a splitter that trims whitespace from results. 返回从结果中修剪空白的分割器。
      Returns:
      the splitter - 分割器
    • trimResults

      public Splitter trimResults(Function<String,String> trimFunction)
      Returns a splitter that trims results using the given function. 返回使用给定函数修剪结果的分割器。
      Parameters:
      trimFunction - the trim function - 修剪函数
      Returns:
      the splitter - 分割器
    • limit

      public Splitter limit(int limit)
      Returns a splitter that limits the number of parts. 返回限制部分数量的分割器。
      Parameters:
      limit - the limit - 限制
      Returns:
      the splitter - 分割器
    • split

      public Iterable<String> split(CharSequence input)
      Splits the given string and returns an iterable of parts. 分割给定字符串并返回部分的可迭代对象。
      Parameters:
      input - the input string - 输入字符串
      Returns:
      the iterable of parts - 部分的可迭代对象
    • splitToList

      public List<String> splitToList(CharSequence input)
      Splits the given string and returns a list of parts. 分割给定字符串并返回部分的列表。
      Parameters:
      input - the input string - 输入字符串
      Returns:
      the list of parts - 部分的列表
    • splitToStream

      public Stream<String> splitToStream(CharSequence input)
      Splits the given string and returns a stream of parts. 分割给定字符串并返回部分的流。
      Parameters:
      input - the input string - 输入字符串
      Returns:
      the stream of parts - 部分的流
    • withKeyValueSeparator

      public Splitter.MapSplitter withKeyValueSeparator(char separator)
      Returns a map splitter using the given key-value separator. 返回使用给定键值分隔符的映射分割器。
      Parameters:
      separator - the key-value separator - 键值分隔符
      Returns:
      the map splitter - 映射分割器
    • withKeyValueSeparator

      public Splitter.MapSplitter withKeyValueSeparator(String separator)
      Returns a map splitter using the given key-value separator. 返回使用给定键值分隔符的映射分割器。
      Parameters:
      separator - the key-value separator - 键值分隔符
      Returns:
      the map splitter - 映射分割器
    • withKeyValueSeparator

      public Splitter.MapSplitter withKeyValueSeparator(Splitter kvSplitter)
      Returns a map splitter using the given splitter for key-value pairs. 返回使用给定分割器处理键值对的映射分割器。
      Parameters:
      kvSplitter - the key-value splitter - 键值分割器
      Returns:
      the map splitter - 映射分割器