Class FluentString

java.lang.Object
com.sure.tool.fluent.FluentString

public final class FluentString extends Object
链式字符串管道(Fluent API 全面化首项)。

与 StrUtil 静态方法形态并存:Fluent 形态支持「校验 → 变换 → 终端」 一条链完成,中间操作不断流、每次返回新的 FluentString(内部 String 不可变,仿 Guava Immutable 思路)。

示例:

String s = Fluent.string("  hello world  ")
        .trim().upperCase().replace("WORLD", "suretool").get();
// "HELLO suretool"
Since:
1.19.0
  • Method Details

    • trim

      public FluentString trim()
      去除首尾空白。

      示例:

      String s = Fluent.string("  x  ").trim().get(); // "x"
      
      Returns:
      新的管道实例
    • upperCase

      public FluentString upperCase()
      转大写(默认 Locale.ROOT,避免土耳其 i 问题)。

      示例:

      String s = Fluent.string("abc").upperCase().get(); // "ABC"
      
      Returns:
      新的管道实例
    • lowerCase

      public FluentString lowerCase()
      转小写(默认 Locale.ROOT)。

      示例:

      String s = Fluent.string("ABC").lowerCase().get(); // "abc"
      
      Returns:
      新的管道实例
    • reverse

      public FluentString reverse()
      反转字符串。

      示例:

      String s = Fluent.string("abc").reverse().get(); // "cba"
      
      Returns:
      新的管道实例
    • replace

      public FluentString replace(String oldChar, String newChar)
      字面替换(非正则)。

      示例:

      String s = Fluent.string("a.b.c").replace(".", "-").get(); // "a-b-c"
      
      Parameters:
      oldChar - 被替换的字面串,不能为 null
      newChar - 替换串,不能为 null
      Returns:
      新的管道实例
      Throws:
      NullPointerException - oldChar 或 newChar 为 null
    • replaceAll

      public FluentString replaceAll(String regex, String replacement)
      正则替换。

      示例:

      String s = Fluent.string("a1b2").replaceAll("\\d", "#").get(); // "a#b#"
      
      Parameters:
      regex - 正则表达式,不能为 null
      replacement - 替换串,不能为 null
      Returns:
      新的管道实例
      Throws:
      NullPointerException - regex 或 replacement 为 null
      IllegalArgumentException - regex 非法
    • substring

      public FluentString substring(int begin)
      截取子串(从 begin 到末尾)。

      示例:

      String s = Fluent.string("suretool").substring(4).get(); // "tool"
      
      Parameters:
      begin - 起始索引(含),越界抛 StringIndexOutOfBoundsException
      Returns:
      新的管道实例
    • substring

      public FluentString substring(int begin, int end)
      截取子串([begin, end))。

      示例:

      String s = Fluent.string("suretool").substring(0, 4).get(); // "sure"
      
      Parameters:
      begin - 起始索引(含)
      end - 结束索引(不含),越界抛 StringIndexOutOfBoundsException
      Returns:
      新的管道实例
    • padLeft

      public FluentString padLeft(int length, char padChar)
      左侧补齐到指定长度。

      示例:

      String s = Fluent.string("7").padLeft(3, '0').get(); // "007"
      
      Parameters:
      length - 目标长度,小于当前长度时返回原串
      padChar - 补齐字符
      Returns:
      新的管道实例
    • padRight

      public FluentString padRight(int length, char padChar)
      右侧补齐到指定长度。

      示例:

      String s = Fluent.string("7").padRight(3, '0').get(); // "700"
      
      Parameters:
      length - 目标长度,小于当前长度时返回原串
      padChar - 补齐字符
      Returns:
      新的管道实例
    • repeat

      public FluentString repeat(int count)
      重复当前字符串 count 次。

      示例:

      String s = Fluent.string("ab").repeat(3).get(); // "ababab"
      
      Parameters:
      count - 重复次数,负数抛 IAE;0 返回空串
      Returns:
      新的管道实例
      Throws:
      IllegalArgumentException - count < 0
    • stripPrefix

      public FluentString stripPrefix(String prefix)
      去除指定前缀(不匹配返回原串)。

      示例:

      String s = Fluent.string("io.github.tasure").stripPrefix("io.").get(); // "github.tasure"
      
      Parameters:
      prefix - 前缀,不能为 null
      Returns:
      新的管道实例
      Throws:
      NullPointerException - prefix 为 null
    • stripSuffix

      public FluentString stripSuffix(String suffix)
      去除指定后缀(不匹配返回原串)。

      示例:

      String s = Fluent.string("report.md").stripSuffix(".md").get(); // "report"
      
      Parameters:
      suffix - 后缀,不能为 null
      Returns:
      新的管道实例
      Throws:
      NullPointerException - suffix 为 null
    • notBlank

      public FluentString notBlank()
      链中校验点:当前值为空白(null 语义由创建时拒绝)抛 IAE。

      示例:

      String name = Fluent.string(rawName).trim().notBlank().get();
      
      Returns:
      当前管道实例(便于继续链式)
      Throws:
      IllegalArgumentException - 当前值为空串或纯空白
    • get

      public String get()
      终端:返回当前字符串。
      Returns:
      当前字符串
    • length

      public int length()
      终端:字符串长度。
      Returns:
      长度
    • isEmpty

      public boolean isEmpty()
      终端:是否为空串。
      Returns:
      空串返回 true
    • isBlank

      public boolean isBlank()
      终端:是否为空白(空串或全空白)。
      Returns:
      空白返回 true
    • startsWith

      public boolean startsWith(String prefix)
      终端:是否以指定前缀开头。
      Parameters:
      prefix - 前缀,不能为 null
      Returns:
      是返回 true
      Throws:
      NullPointerException - prefix 为 null
    • endsWith

      public boolean endsWith(String suffix)
      终端:是否以指定后缀结尾。
      Parameters:
      suffix - 后缀,不能为 null
      Returns:
      是返回 true
      Throws:
      NullPointerException - suffix 为 null
    • contains

      public boolean contains(String fragment)
      终端:是否包含指定子串。
      Parameters:
      fragment - 子串,不能为 null
      Returns:
      包含返回 true
      Throws:
      NullPointerException - fragment 为 null
    • equalsIgnoreCase

      public boolean equalsIgnoreCase(String other)
      终端:忽略大小写比较。
      Parameters:
      other - 比较对象,null 返回 false
      Returns:
      忽略大小写相等返回 true
    • toString

      public String toString()
      Overrides:
      toString in class Object