0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Java】文字列を結合する方法(join)

Posted at

はじめに

本記事ではJava8で追加されたStringクラスのjoinメソッドについてアウトプットします。

#使い方
・指定された引数で文字列を結合します。
・1つ目の引数は区切り文字を指定し、2つ目の引数に結合する文字列を指定します。任意の数を指定できます。
・文字列の結合時に新たなインスタンスは生成されます。(StringBuilderを使うよりも遅くなる)
・文字列がnullの場合、nullという文字が追加されます。

public static String join(CharSequence delimiter,CharSequence... elements)
public class Sample {
    public static void main(String[] args) {
          
        String a = String.join("","A","BC","D");
        System.out.println(a);// ABCD
        
        String b = String.join("-","A","BC","D");
        System.out.println(b);// A-BC-D
        
        // 配列の各値を結合
        String[] sports = {"soccer","baseball","basketball"};
        String c = String.join("-",sports);
        System.out.println(c);// soccer-baseball-basketball

    }
}

#参照
Java SE Platform SE 8 #join

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?