1. 程式人生 > >StringUtils常用方法(七)

StringUtils常用方法(七)

如果你的專案是maven工程,首先在pom.xml中新增commons-lang3包的依賴 

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.4</version>
</dependency>

是否包含某個字元

import org.apache.commons.lang3.StringUtils;

StringUtils.contains("abc", "")   = true
StringUtils.contains("abc", "a")  = true
StringUtils.contains("abc", "A") = true
StringUtils.contains("abc", 'a') = true
StringUtils.contains("abc", 'z') = false
//檢查字元序列(CharSequence)是否包含給定陣列中的任何字元序列
//boolean containsAny(final CharSequence cs, final char... searchChars)
StringUtils.containsAny("zzabyycdxx", "za") = true
StringUtils.containsAny("zzabyycdxx", "by") = true
StringUtils.containsAny("abcd", "ab", "cd") = true
StringUtils.containsAny("abc", "d", "abc")  = true
StringUtils.containsAny("zzabyycdxx",'z','a') = true
StringUtils.containsAny("zzabyycdxx",'b','y') = true
StringUtils.containsAny("aba", 'z')           = false
//檢查字元序列(CharSequence)是否只包含某些字元
//boolean containsOnly(final CharSequence cs, final char... valid)
StringUtils.containsOnly("ab", '')      = false
StringUtils.containsOnly("abab", 'abc') = true
StringUtils.containsOnly("ab1", 'abc')  = false
StringUtils.containsOnly("abz", 'abc')  = false
//檢查字元序列(CharSequence)不包含某些字元 
//boolean containsNone(final CharSequence cs, final char... searchChars)
StringUtils.containsNone("ab","")       = true
StringUtils.containsNone("abab","xyz")  = true
StringUtils.containsNone("ab1", "xyz")  = true
StringUtils.containsNone("abz", "xyz")  = false
//檢查字元序列是否以指定的字首開始
StringUtils.startsWith("abcdef", null)  = false
StringUtils.startsWith("abcdef", "abc") = true
StringUtils.startsWith("ABCDEF", "abc") = false