자바 String 숫자를 자릿수 int 배열로 분할

Jmnote (토론 | 기여)님의 2019년 1월 1일 (화) 09:46 판

1 개요

자바 String 숫자를 자릿수별 int 배열로 분할
import java.util.Arrays;
import java.util.stream.Stream; 
public class MyClass {
    public static void main(String args[]) {
        String str = "12345";
        int[] digits = Stream.of(str.split("")).mapToInt(Integer::parseInt).toArray();
        System.out.println( Arrays.toString(digits) );
        // [1, 2, 3, 4, 5]
    }
}
import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
        String str = "12345";
        int[] digits = new int[str.length()];
        for(int i=0; i<str.length(); i++) digits[i] = str.charAt(i) - '0';
        System.out.println( Arrays.toString(digits) );
        // [1, 2, 3, 4, 5]
    }
}

2 같이 보기

3 참고