「Java8」joinメソッドで区切文字と配列を連結するサンプル

環境
JavaSE 1.8
Eclipse 2019

書式
String String.join(区切り文字, 配列)
引数1は区切り文字を指定しします
引数2はString型の 配列を指定します

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class StaticFinalDemo {
// コロン
private static final String PTN = ":";
// カンマ
private static final String PTN2 = ",";
public static void main(String[] args) {
String cityArr[] = new String[] {"Tokyo", "oosak", "fukuoka"};
String res = String.join(PTN, cityArr);
System.out.println("結果1: " + res);
String cityArr2[] = new String[] {"東京", "大阪", "福岡"};
String res2 = String.join(PTN2, cityArr2);
System.out.println("結果2: " + res2);
}
}
package com.arkgame.study; public class StaticFinalDemo { // コロン private static final String PTN = ":"; // カンマ private static final String PTN2 = ","; public static void main(String[] args) { String cityArr[] = new String[] {"Tokyo", "oosak", "fukuoka"}; String res = String.join(PTN, cityArr); System.out.println("結果1: " + res); String cityArr2[] = new String[] {"東京", "大阪", "福岡"}; String res2 = String.join(PTN2, cityArr2); System.out.println("結果2: " + res2); } }
package com.arkgame.study;

public class StaticFinalDemo {

      // コロン
      private static final String PTN = ":";
      // カンマ
      private static final String PTN2 = ",";

      public static void main(String[] args) {
            String cityArr[] = new String[] {"Tokyo", "oosak", "fukuoka"};
            String res = String.join(PTN, cityArr);
            System.out.println("結果1: " + res);
            
            String cityArr2[] = new String[] {"東京", "大阪", "福岡"};
            String res2 = String.join(PTN2, cityArr2);
            System.out.println("結果2: " + res2);
      }
}

実行結果
結果1: Tokyo:oosak:fukuoka
結果2: 東京,大阪,福岡

Java

Posted by arkgame