Strings

JavaDoc

文字列の空とかnullとか

nullか空は、どっちかに統一した方がいい。
Strings.emptyToNull(String string)
Strings.nullToEmpty(String string)

でも、優柔不断で選べないなら、両方チェックするといい。よくある !=null || string.isEmpty()より若干見やすい。(ちなみにisEmptyはJava 6のメソッド)
Strings.isNullOrEmpty(String string)

その他


package test;

import com.google.common.base.Strings;

public class StringsTest {
   
    public static void main(String[] args) {
       
        //数字の桁あわせ
        String ret=Strings.padStart(""+8, 3, '0');
        System.out.println(ret);
       
        //仮テキストの作成
        System.out.println(Strings.repeat(Strings.repeat("test ",4)+"\n", 5));
    }

}

これの出力は
008
test test test test
test test test test
test test test test
test test test test
test test test test

数字を桁合わせとか、1行でかけて便利かも。

仮テキストなんてめったに作らないけど、シンプルにかける。



Comments