前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Guava-1.22类Strings

Guava-1.22类Strings

作者头像
悠扬前奏
发布2019-05-31 10:26:34
5660
发布2019-05-31 10:26:34
举报
文章被收录于专栏:悠扬前奏的博客

全路径名:

com.google.common.base ** Strings**

简介

静态实用方法,属于String或者CharSequence实例。

方法

nullToEmpty
代码语言:javascript
复制
public static String nullToEmpty(@NullableDecl String string)

返回字符串非null,返回原字符串。否则返回空字符串。

emptyToNull
代码语言:javascript
复制
@NullableDecl
public static String emptyToNull(@NullableDecl String string)

返回字符串非空字符串,返回原字符串。否则,返回null。

isNullOrEmpty
代码语言:javascript
复制
public static boolean isNullOrEmpty(@NullableDecl String string)

判断字符串是不是null或者空字符串。

padStart/padEnd
代码语言:javascript
复制
public static String padStart(String string, int minLength, char padChar)
public static String padEnd(String string, int minLength, char padChar)

返回字符串,长度最少是minLength,长度不够的话用重复的padChar填充。

repeat
代码语言:javascript
复制
public static String repeat(String string, int count)

返回string重复count次。

commonPrefix/commonSuffix
代码语言:javascript
复制
public static String commonPrefix(CharSequence a, CharSequence b)
public static String commonSuffix(CharSequence a, CharSequence b) 

返回共同的前缀/后缀字符串。

例子

代码语言:javascript
复制
public class StringTest {
    public static void main(String[] args) {
        String s = "hello world";
        String e = "";
        String n = null;
        System.out.println(Strings.nullToEmpty(s)); // "hello world"
        System.out.println(Strings.nullToEmpty(e)); // ""
        System.out.println(Strings.nullToEmpty(n)); // ""
        System.out.println(Strings.emptyToNull(s)); // "hello world"
        System.out.println(Strings.emptyToNull(e)); // null
        System.out.println(Strings.emptyToNull(n)); // null
        System.out.println(Strings.isNullOrEmpty(s)); // false
        System.out.println(Strings.isNullOrEmpty(e)); // true
        System.out.println(Strings.isNullOrEmpty(n)); // true
        System.out.println(Strings.padStart("xx", 4, 'o')); // "ooxx"
        System.out.println(Strings.padEnd("xx", 4, 'o')); // "xxoo"
        System.out.println(Strings.repeat("xx", 3)); // "xxxxxx"
        System.out.println(Strings.commonPrefix("oooo", "ooxx")); // "oo"
        System.out.println(Strings.commonSuffix("xxxx", "ooxx")); // "xx"
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.05.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 全路径名:
  • 简介
  • 方法
    • nullToEmpty
      • emptyToNull
        • isNullOrEmpty
          • padStart/padEnd
            • repeat
              • commonPrefix/commonSuffix
              • 例子
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档