前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 荐 OSGL 工具库 - Java 字串

原 荐 OSGL 工具库 - Java 字串

作者头像
老码农
发布2018-06-20 17:05:05
6170
发布2018-06-20 17:05:05
举报
文章被收录于专栏:老码农专栏老码农专栏

在 [OSGL 工具库 - 图片处理的艺术] (https://my.oschina.net/greenlaw110/blog/1786151) 中我们介绍了如何通过 OSGL Img 提供的一套 API 流畅地处理图片, 例如:

代码语言:javascript
复制
source(img1())
        .resize(300, 400)
        .pipeline()
        .crop(50, 50, 250, 350)
        .pipeline()
        .watermark("HELLO OSGL")
        .writeTo("/tmp/img1_pipeline.png");

采用这种方式编写的代码极大地提高了可读性, 我们把这种 API 结构称为流式编程 (Fluent Programming). 我们在本文中将介绍如何使用 OSGL S 库处理字串, 包括采用流式编程来提供具有高可读性的字串处理代码.

下面用代码来说明一切:

1. 字串判断

1.1 判断字串是否为空

代码语言:javascript
复制
boolean b = S.isEmpty(""); // true
b = S.isEmpty(null); // true
b = S.isEmpty(" "); // false
b = S.isBlank(" "); // true
b = S.isNotEmpty("abc"); // true
b = S.isNotBlank("\t"); false
b = S.isAnyEmpty("", "xyz", ..); // true
b = S.isAllEmpty("", "xyz", ...); // false

1.2. 判断字串是否为数字

代码语言:javascript
复制
boolean b = S.isIntOrLong("12345"); // true
b = S.isIntOrLong("1234.5"); // false
b = S.isNumeric("1234.5"); // true

1.3 字串判断的流式编程

代码语言:javascript
复制
boolean b = S.is("foo").empty(); // false
b = S.is(" ").blank(); // true
b = S.is("abc").contains("ab"); // true
b = S.is("abc").startsWith("ab"); // true
b = S.is("abc").endsWith("ab"); // false
b = S.is(null).equalsTo(null); // true
b = S.is(null).equalsTo(""); // true
b = S.is(null).equalsTo(" "); // false
b = S.is("[abc]").wrappedWith("[", "]"); // true
b = S.is("<abc>").wrappedWith(S.ANGLE_BRACKETS); // true

2. 字串相等性

代码语言:javascript
复制
yes(S.eq("foo", "foo"));
yes(S.eq("foo", "Foo", S.IGNORECASE));
no(S.eq("foobar", " FooBar "));
yes(S.eq("foobar", " FooBar ", S.IGNORESPACE | S.IGNORECASE));
yes(S.eq(null, null));
no(S.eq(null, "foo"));

3. 字串格式化

代码语言:javascript
复制
String s = S.fmt("hello %s", "world"); // hello world
s = S.msgFmt("hello {0}", "world"); // hello world

4. 字串验证

代码语言:javascript
复制
String s = S.requireNotEmpty("foo"); // foo
s = S.requireNotBlank("bar"); // bar
s = S.requireNotEmpty(""); // raise IllegalArgumentException
s = S.requireNotBlank("  "); raise IllegalArgumentException

5. 字串长度

代码语言:javascript
复制
int n = S.len("abc"); // 3
n = S.len(null); // 0

6. 字串分割

代码语言:javascript
复制
List<String> list = S.fastSplit("/tmp/foo/bar", "/"); [tmp, foo, bar]
S.List list = S.split("abc5xyz132ijk", ":"); [abc, xyz, ijk]
list = S.split("/tmp/foo/bar").by("/").get(); [tmp, foo, bar]
list = S.split("[abc]-[xyz]").by("-").stripElementWrapper(S.BRACKETS).get(); // [abc, xyz]

7. 字串拼接

7.1 集合拼接为字串

代码语言:javascript
复制
List<String> list = C.list("abc", "xyz");
String s = S.join(list).by("-").get(); // abc-xyz
s = S.join(list).by("-").wrapElementWith(S.SQUARE_BRACKETS).get(); // [abc]-[xyz]
list = S.list("abc", null, "xyz");
S.join(list).by("-").get(); // abc--xyz
S.join(list).by("-").ignoreEmptyElement().get(); // abc-xyz

7.2 任意类型对象拼接为字串

代码语言:javascript
复制
String s = S.concat("abc", 123); // abc123
s = S.concat("abc", null, 123); // abc123

7.3 路径拼接

代码语言:javascript
复制
eq("foo/bar", S.pathConcat("foo", '/', "bar"));
eq("foo/bar", S.pathConcat("foo/", '/', "bar"));
eq("foo/bar", S.pathConcat("foo", '/', "/bar"));
eq("foo/bar", S.pathConcat("foo/", '/', "/bar"));

9. 字串修饰确认

代码语言:javascript
复制
eq("[abc]", S.ensure("abc").wrappedWith(S.SQUARE_BRACKETS));
eq("[abc]", S.ensure("[abc").wrappedWith(S.SQUARE_BRACKETS));
eq("[abc]", S.ensure("[abc]").wrappedWith(S.SQUARE_BRACKETS));
eq("abc", S.ensure("abc").strippedOff(S.ANGLE_BRACKETS));
eq("abc", S.ensure("<abc>").strippedOff(S.ANGLE_BRACKETS));
eq("_abc", S.ensure("abc").startWith('_'));
eq("abc.html", S.ensure("abc").endWith(".html"));

10. 字串替换

代码语言:javascript
复制
String s;
s = S.given("hello world").replace("world").with("foo"); // hello foo
s = S.replace("world").in("hello world").with("foo"); // hello foo
s = S.replace("world").with("foo").in("hello world"); // hello foo
s = S.replace("[0-9]+").with("[N]").usingRegEx().in("times 10")); // times [N]

11. 字串重复

代码语言:javascript
复制
eq("aaa", S.repeat('a').times(3));
eq("aaa", S.repeat('a').x(3));
eq("aaaaa", S.repeat('a').forFiveTimes());
eq("foofoo", S.repeat("foo").times(2));
eq("foofoo", S.repeat("foo").x(2));

12. 字串包裹

代码语言:javascript
复制
eq("*abc*", S.wrap("abc").with("*"));
eq("[abc]", S.wrap("abc").with("[", "]"));
eq("[abc]", S.wrap("abc").with(S.BRACKETS));
eq("(abc)", S.wrap("abc").with(S.PARENTHESES));
eq("<abc>", S.wrap("abc").with(S.DIAMOND));
eq("<abc>", S.wrap("abc").with(S.ANGLE_BRACKETS));
eq("《abc》", S.wrap("abc").with(S.书名号));

13. 字串去包裹

代码语言:javascript
复制
eq("abc", S.strip("[abc]").of(S.BRACKETS));
eq("abc", S.strip("<abc>").of(S.DIAMOND));
eq("abc", S.strip("*abc*").of("*"));
eq("abc", S.strip("111abc222").of("111", "222"));

14. 字串切断

代码语言:javascript
复制
eq("abc12", S.cut("abc123").by(5));
eq("ab", S.cut("abc123").first(2));
eq("23", S.cut("abc123").last(2));
eq("123", S.cut("abc123").after("abc"));
eq("abc", S.cut("abc123").before("123"));
eq("abc", S.cut("abc123abc123").before("123"));
eq("abc", S.cut("abc123abc123").beforeFirst("123"));
eq("abc123abc", S.cut("abc123abc123").beforeLast("123"));
eq("123", S.cut("abc123abc123").after("abc"));
eq("123", S.cut("abc123abc123").afterLast("abc"));
eq("123abc123", S.cut("abc123abc123").afterFirst("abc"));

15. 关键字处理

代码语言:javascript
复制
final String s = "Hello World";
eq("HelloWorld", S.camelCase(s));
eq("hello_world", S.underscore(s));
eq("hello-world", S.dashed(s));
eq("Hello World", S.capFirst(s));
eq("hello World", S.lowerFirst(s));
eq("Hello-World", Keyword.of(s).httpHeader());
eq("helloWorld", Keyword.of(s).javaVariable());
eq("HELLO_WORLD", Keyword.of(s).constantName());
eq("Hello world", Keyword.of(s).readable());

16. 子串计数

代码语言:javascript
复制
final String s = "1011101111";
eq(3, S.count("11").in(s));
eq(5, S.count("11").withOverlap().in(s));

17. 其他工具

代码语言:javascript
复制
eq("", S.trim(null));
eq("abc", S.trim(" abc"));
eq("abc\nxyz", S.dos2unix("abc\n\rxyz"));
eq("abc\n\rxyz", S.unix2dos("abc\nxyz"));
eq("this...", S.maxLength("this is a long text", 4));
yes(S.eq("foo", "foo"));
String s;
s = S.uuid(); // 9b2ec83d-15df-4746-9689-c82df5643832
s = S.random(); //kGYH$KCj
s = S.random(2); // gb
s = S.maxLength("this is a long text", 4); // this...
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 字串判断
    • 1.1 判断字串是否为空
      • 1.2. 判断字串是否为数字
        • 1.3 字串判断的流式编程
        • 2. 字串相等性
        • 3. 字串格式化
        • 4. 字串验证
        • 5. 字串长度
        • 6. 字串分割
        • 7. 字串拼接
          • 7.1 集合拼接为字串
            • 7.2 任意类型对象拼接为字串
              • 7.3 路径拼接
              • 9. 字串修饰确认
              • 10. 字串替换
              • 11. 字串重复
              • 12. 字串包裹
              • 13. 字串去包裹
              • 14. 字串切断
              • 15. 关键字处理
              • 16. 子串计数
              • 17. 其他工具
              相关产品与服务
              图片处理
              图片处理(Image Processing,IP)是由腾讯云数据万象提供的丰富的图片处理服务,广泛应用于腾讯内部各产品。支持对腾讯云对象存储 COS 或第三方源的图片进行处理,提供基础处理能力(图片裁剪、转格式、缩放、打水印等)、图片瘦身能力(Guetzli 压缩、AVIF 转码压缩)、盲水印版权保护能力,同时支持先进的图像 AI 功能(图像增强、图像标签、图像评分、图像修复、商品抠图等),满足多种业务场景下的图片处理需求。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档