LoginSignup
1
0

More than 5 years have passed since last update.

Java8のお勉強(StringJoinerとjoin)

Last updated at Posted at 2017-10-15

サンプル

import java.util.StringJoiner;

/**
 * Stringのjoinメソッドと、StringJoinerの勉強
 * @author komikcomik
 *
 */
public class JoinTest {

    public static void main(String[] args) {

        String[] strs = {"a","b","c"};
        String joinStr = String.join(",", strs);
        System.out.println(joinStr);

        StringJoiner sj = new StringJoiner(",");
        joinStr = sj.add("X").add("Y").add("Z").toString();
        System.out.println(joinStr);
    }

}

結果

a,b,c
X,Y,Z

定型フォーマットに整形したい場合なんかに便利かもですね。

1
0
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
1
0