Skip to content

Instantly share code, notes, and snippets.

@YukiYoshikawa
Last active December 16, 2015 18:28
Show Gist options
  • Save YukiYoshikawa/5477450 to your computer and use it in GitHub Desktop.
Save YukiYoshikawa/5477450 to your computer and use it in GitHub Desktop.
package trial.yy.guava.client.base;
import com.google.common.base.Strings;
/**
* com.google.common.base.Stringsを試すためのサンプル
* User: yy
*/
public class StringsClient {
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.nullToEmpty 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") + "]");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment