Guavaのcom.google.common.base.Strings
の利用サンプルです。
Stringsクラスは文字列に関するユーティリティメソッドを提供してくれます。
サンプルコードで試したのは以下の通りです。
指定回数繰り返した文字列を取得
■API
Strings#repeat
■サンプル
System.out.println("## Strings.repeat execute"); System.out.println(Strings.repeat("a", 10)); System.out.println(Strings.repeat("hello", 3));
■実行結果
## Strings.repeat execute aaaaaaaaaa hellohellohello
Left Pad
■API
Strings#padStart
■サンプル
System.out.println("## Strings.padStart execute"); System.out.println(Strings.padStart("123", 5, '0')); System.out.println(Strings.padStart("abcde", 3, '*'));
■実行結果
## Strings.padStart execute 00123 abcde
Right Pad
■API
Strings#padEnd
■サンプル
System.out.println("## Strings.padEnd execute"); System.out.println(Strings.padEnd("123.1", 7, '0')); System.out.println(Strings.padEnd("abcde", 3, '*'));
■実行結果
## Strings.padEnd execute 123.100 abcde
指定文字列が空またはnullかどうかをチェック
■API
Strings#isNullOrEmpty
■サンプル
System.out.println("## Strings.isNullOrEmpty execute"); System.out.println("null = " + Strings.isNullOrEmpty(null)); System.out.println("empty string = " + Strings.isNullOrEmpty("")); System.out.println("hello = " + Strings.isNullOrEmpty("hello"));
■実行結果
## Strings.isNullOrEmpty execute null = true empty string = true hello = false
nullを空文字列にする
■API
Strings#nullToEmpty
■サンプル
System.out.println("## Strings.nullToEmpty execute"); System.out.println("null -> [" + Strings.nullToEmpty(null) + "]"); System.out.println("hello -> [" + Strings.nullToEmpty("hello") + "]");
■実行結果
## Strings.nullToEmpty execute null -> [] hello -> [hello]
空文字列をnullにする
■API
Strings#emptyToNull
■サンプル
System.out.println("## Strings.emptyToNull execute"); System.out.println("empty string -> [" + Strings.emptyToNull("") + "]"); System.out.println("hello -> [" + Strings.emptyToNull("hello") + "]");
■実行結果
## Strings.emptyToNull execute empty string -> [null] hello -> [hello]
2つの文字列の共通のprefix取得
■API
Strings#commonPrefix
■サンプル
System.out.println("## Strings.commonPrefix execute"); System.out.println("foo_123456.txt & foo_234567.txt common prefix is [" + Strings.commonPrefix("foo_123456.txt", "foo_234567.txt") + "]"); System.out.println("20130429 & 20130430 common prefix is [" + Strings.commonPrefix("20130429", "20130430") + "]"); System.out.println("abc & def common prefix is [" + Strings.commonPrefix("abc", "def") + "]");
■実行結果
## Strings.commonPrefix execute foo_123456.txt & foo_234567.txt common prefix is [foo_] 20130429 & 20130430 common prefix is [201304] abc & def common prefix is []
2つの文字列の共通のsuffix取得
■API
Strings#commonSuffix
■サンプル
System.out.println("## Strings.commonSuffix execute"); System.out.println("foo_123456.txt & foo_234567.txt common suffix is [" + Strings.commonSuffix("foo_123456.txt", "foo_234567.txt") + "]"); System.out.println("20130429 & 20120429 common suffix is [" + Strings.commonSuffix("20130429", "20120429") + "]"); System.out.println("abc & def common suffix is [" + Strings.commonSuffix("abc", "def") + "]");
■実行結果
## Strings.commonSuffix execute foo_123456.txt & foo_234567.txt common suffix is [.txt] 20130429 & 20120429 common suffix is [0429] abc & def common suffix is []
試したソースコードの全体は以下です。
import com.google.common.base.Strings; public class StringsTrial { public static void main(String[] args) { // 指定の文字列を指定回数繰り返す System.out.println("## Strings.repeat execute"); System.out.println(Strings.repeat("a", 10)); System.out.println(Strings.repeat("hello", 3)); // 文字列の先頭を指定文字で埋める(left pad) System.out.println("## Strings.padStart execute"); System.out.println(Strings.padStart("123", 5, '0')); System.out.println(Strings.padStart("abcde", 3, '*')); // 文字列の末尾を指定文字で埋める(right pad) System.out.println("## Strings.padEnd execute"); System.out.println(Strings.padEnd("123.1", 7, '0')); System.out.println(Strings.padEnd("abcde", 3, '*')); // nullまたは空文字列(length == 0)かどうかをチェックする System.out.println("## Strings.isNullOrEmpty execute"); System.out.println("null = " + Strings.isNullOrEmpty(null)); System.out.println("empty string = " + Strings.isNullOrEmpty("")); System.out.println("hello = " + Strings.isNullOrEmpty("hello")); // nullの場合空文字列を返す System.out.println("## Strings.nullToEmpty execute"); System.out.println("null -> [" + Strings.nullToEmpty(null) + "]"); System.out.println("hello -> [" + Strings.nullToEmpty("hello") + "]"); // 空文字列の場合nullを返す System.out.println("## Strings.emptyToNull execute"); System.out.println("empty string -> [" + Strings.emptyToNull("") + "]"); System.out.println("hello -> [" + Strings.emptyToNull("hello") + "]"); // 指定文字列同士の共通のprefixを返す System.out.println("## Strings.commonPrefix execute"); System.out.println("foo_123456.txt & foo_234567.txt common prefix is [" + Strings.commonPrefix("foo_123456.txt", "foo_234567.txt") + "]"); System.out.println("20130429 & 20130430 common prefix is [" + Strings.commonPrefix("20130429", "20130430") + "]"); System.out.println("abc & def common prefix is [" + Strings.commonPrefix("abc", "def") + "]"); // 指定文字列同士の共通のsuffixを返す System.out.println("## Strings.commonSuffix execute"); System.out.println("foo_123456.txt & foo_234567.txt common suffix is [" + Strings.commonSuffix("foo_123456.txt", "foo_234567.txt") + "]"); System.out.println("20130429 & 20120429 common suffix is [" + Strings.commonSuffix("20130429", "20120429") + "]"); System.out.println("abc & def common suffix is [" + Strings.commonSuffix("abc", "def") + "]"); } }