[JAVA] 배열을 문자열로 바꾸기

김재중·2023년 8월 27일
0
  • String.join("구분자","대상 list or array")
  • 문자열을 연결하는 java 메서드

String.join 사용예시

import java.util.ArrayList;
import java.util.List;

public class StringJoinExample {
    public static void main(String[] args) {
    
        String[] fruits = {"Apple", "Banana", "Orange"};
        String joinedFruits = String.join(", ", fruits);
        // 배열의 요소를 구분자로 연결
        System.out.println("Joined fruits: " + joinedFruits);
        // Joined fruits: Apple, Banana, Orange

        List<String> colors = new ArrayList<>();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        String joinedColors = String.join(" - ", colors);
        // 리스트의 요소를 구분자로 연결
        System.out.println("Joined colors: " + joinedColors);
        // Joined colors: Red - Green - Blue
    }
}
profile
안녕하세요!

0개의 댓글