很久没有使用过 Google Guava的,在这里做一个总结,准备做一个系列。能想到哪里就写到哪里吧。先写一下对字符串的处理。开始~~~~

package com.cmcc.family.utils

import com.google.common.base.Strings
import com.google.common.base.Splitter
import com.google.common.base.Joiner
import com.google.common.base.CharMatcher
/**
* Guava 字符串操作举例
* @author jingshuai
* @date 2016-05-06
*/
object GuavaString {
//字符串处理
def stringIsNullOrEmpty {
println(Strings.isNullOrEmpty(“jingshuai”)); //false
println(Strings.isNullOrEmpty(“”)); //true
println(Strings.isNullOrEmpty(null)); //true
}
//截取相同 前缀 和后缀
def stringCommonPrefixOrSuffix {
val a = “hi,小菜鸟,www.jsledd.cn
val b = “hi,diudiu,www.jsledd.cn
println(“前缀:” + Strings.commonPrefix(a, b)); //hi,
println(“后缀:” + Strings.commonSuffix(a, b)); //,www.jsledd.cn
}
//字符串补全
def stringPad {
val minLength = 5
println(“123前补足0,共5位:” + Strings.padStart(“123”, minLength, ‘0’))
println(“123后补足0,共5位:” + Strings.padEnd(“123”, minLength, ‘0’))
}
//拆分字符串
def stringSplitter {
import scala.collection.JavaConverters._
//拆分成string
Splitter.onPattern(“[,,]”)
.trimResults()
.omitEmptyStrings()
.split(“diudiu,DIUDIU,丢丢,小菜鸟,www.jsledd.cn”).asScala
.foreach((x: String) => println(x + “;”))
//拆分成元组
Splitter.onPattern(“[,,]”)
.trimResults()
.omitEmptyStrings()
.withKeyValueSeparator(“=”)
.split(“name1=diudiu,name2=DIUDIU,name3=丢丢,name4=小菜鸟,web=www.jsledd.cn”).asScala
.foreach((x: (String, String)) => println(x._1 + “=” + x._2))
}
//合并字符串
def stringJoin {
//合并string
println(Joiner.on(“,”).join(“diudiu,DIUDIU”,“丢丢”,“小菜鸟”,“www.jsledd.cn”))
Map(“name1” -> “diudiu”,“name2” -> “DIUDIU”,“web” -> “www.jsledd.cn”)
import scala.collection.JavaConversions.mapAsJavaMap
val map = Map(“name1” -> “diudiu”,“name2” -> “DIUDIU”,“web” -> “www.jsledd.cn”)
println(Joiner.on(“,”).withKeyValueSeparator(“=”).join(map))
}
def stringRepeat {
println(Strings.repeat(“name=diudiu,”, 3))
}
def main(args: Array[String]): Unit = {
//stringIsNullOrEmpty
//stringCommonPrefixOrSuffix
//stringPad
//stringSplitter
//stringJoin
stringRepeat
}
}