import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import com.google.common.base.Strings;/*** TODO 在此写上类的相关说明.<br>* @author gqltt<br>* @version 1.0.0 2021年11月11日<br>* @see * @since JDK 1.5.0*/
public class StringsDemo {/*** @param args*/public static void main(String[] args) {nullToEmpty();tesEmptyToNull();isNullOrEmpty();padStart();padEnd();repeat();commonPrefix();commonSuffix();}/*** null转空串.*/static void nullToEmpty() {Assert.assertThat(Strings.nullToEmpty("foo"), CoreMatchers.is("foo"));Assert.assertThat(Strings.nullToEmpty(null), CoreMatchers.is(""));}/*** 空串转null.*/static void emptyToNull() {Assert.assertThat(Strings.emptyToNull("foo"), CoreMatchers.is("foo"));Assert.assertThat(Strings.emptyToNull(""), CoreMatchers.is(CoreMatchers.nullValue()));Assert.assertThat(Strings.emptyToNull("  "), CoreMatchers.is("  "));}/*** 是否null或空串.*/static void isNullOrEmpty() {Assert.assertThat(Strings.isNullOrEmpty(""), CoreMatchers.is(true));Assert.assertThat(Strings.isNullOrEmpty("  "), CoreMatchers.is(false));Assert.assertThat(Strings.isNullOrEmpty(null), CoreMatchers.is(true));Assert.assertThat(Strings.isNullOrEmpty("foo"), CoreMatchers.is(false));}/*** 左填充.*/static void padStart() {String expected = "001";String returned = Strings.padStart("1", 3, '0');Assert.assertThat(returned, CoreMatchers.is(expected));}/*** 右填充.*/static void padEnd() {String expected = "boom!!";String returned = Strings.padEnd("boom", 6, '!');Assert.assertThat(returned, CoreMatchers.is(expected));}/*** 重复.*/static void repeat() {String result = Strings.repeat("abc", 3);Assert.assertThat(result, CoreMatchers.is("abcabcabc"));}/*** 公共前缀.*/static void commonPrefix() {String result = Strings.commonPrefix("abc12345", "abc44544");Assert.assertThat(result, CoreMatchers.is("abc"));}/*** 公共后缀.*/static void commonSuffix() {String result = Strings.commonSuffix("12345abc", "44544abc");Assert.assertThat(result, CoreMatchers.is("abc"));}
}

Guava入门~Strings相关推荐

  1. google guava 入门教程

    Guava(瓜娃)学习笔记 (代码下载地址) Guava工程包含了若干被google的java项目广泛依赖的核心库,例如:集合 [collections] .缓存 [caching] .原生类型支持 ...

  2. guava入门学习2(新集合)

    新集合类型 这可能是你梦寐以求的集合,在解决瘙痒方面,功能异常强大 2.1 MultiSet 痛点:统计次数 传统做法: Map<String,Integer> count = new H ...

  3. Guava入门~CacheStats

    构建缓存对象时调用recordStats(),指定可以做缓存统计 LoadingCache<String,TradeAccount> tradeAccountCache = CacheBu ...

  4. Guava入门~Lists

    import java.util.List; import org.hamcrest.CoreMatchers; import org.junit.Assert; import com.google. ...

  5. Guava入门~MoreObjects

    import java.math.BigDecimal; import org.hamcrest.CoreMatchers; import org.junit.Assert; import com.g ...

  6. Guava入门~Objects

    import java.util.Date; import org.hamcrest.CoreMatchers; import org.junit.Assert; import com.google. ...

  7. Guava入门~Charsets

    import java.io.UnsupportedEncodingException; import org.hamcrest.CoreMatchers; import org.junit.Asse ...

  8. Guava入门~CharMatcher

    import org.hamcrest.CoreMatchers; import org.junit.Assert; import com.google.common.base.CharMatcher ...

  9. Guava入门~Splitter

    String.split()问题:中间保留,最后丢弃 String commaSeparatedString = "Foo,,Bar,,Baz,,,"; String[] word ...

最新文章

  1. 关于SSH远程连接报错
  2. Scala模式匹配的亮点——Martin Odersky访谈(四)
  3. 非此即彼的逻辑错误_MBA逻辑攻略-逻辑知识大全,快来收藏吧!
  4. 金额大小写转换(1)
  5. show attend and tell 计算bleu分数(1到4)
  6. C#语法糖 Null 条件运算符 【?.】
  7. 淘宝网商品管理?技术 ?
  8. java实现高性能的数据同步
  9. python unittest教程_python unittest 基本介绍
  10. Java高级:面试题-1
  11. 腾讯联手联通推出车联网“网卡”,打“内容”+“流量”的组合拳...
  12. SPSS教程-t检验怎么做?
  13. SVN下载 1.11.0版本 win7 32位 安装文件及中文语言包
  14. windows版本修改,家庭版改专业版,专业版改教育版,或者是改家庭版
  15. python计算平均值画折线图_【中年阿姨python入门】绘制折线图
  16. SpringBoot后台java下载文件及注意的地方
  17. SpringCloud微服务项目实战 - 6.延迟任务
  18. CutPaste: Self-Supervised Learning for Anomaly Detection and Localization 全文翻译+详细解读
  19. 全球排名前十的美资投行由于全球业务增长速度较快,需要及时补充技术人员,将于3月4日(周六)在其上海office举办Super Saturday的招聘活动,招聘33名Java开发工程师。欢迎积极报名并推
  20. 华为设备配置Hub and Spoke

热门文章

  1. 18-Gm-TransH:Group-Constrained Embedding of Multi-fold Relations in Knowledge Bases,嵌入,transH,n-ary
  2. python enumerate用法总结(转)
  3. day36-表关系(外键)
  4. springCloud(微服务的概念)1-1
  5. JOptionPane弹框常用实例
  6. Git从零教你入门(4):Git服务之 gogs部署安装
  7. 动态规划算法的应用模型
  8. HDU 5600(瞎搞)
  9. php中isset() , unnset(), empty()函数
  10. frame或者iframe的contentwindow属性