"자바 String을 char 배열로 변환"의 두 판 사이의 차이

18번째 줄: 18번째 줄:


==char 배열로==
==char 배열로==
<syntaxhighlight lang='java'>
<syntaxhighlight lang='java' run>
public class MyClass {
public class MyClass {
     public static void main(String args[]) {
     public static void main(String args[]) {

2021년 4월 11일 (일) 00:25 판

1 개요

자바 String to array
자바 String을 배열로 변환

2 String 배열로

public class MyClass {
    public static void main(String args[]) {
        String str = "ABC안녕123★";
        String[] arr = str.split("(?<!^)");
        for(int i=0; i<arr.length; i++) System.out.printf("%s, ",arr[i]);
        // A, B, C, 안, 녕, 1, 2, 3, ★, 
        System.out.println( java.util.Arrays.toString(arr) );
        // [A, B, C, 안, 녕, 1, 2, 3, ★]
    }
}

3 char 배열로

public class MyClass {
    public static void main(String args[]) {
        String str = "ABC안녕123★";
        char[] chs = str.toCharArray();
        for(int i=0; i<chs.length; i++) System.out.printf("%c, ",chs[i]);
        // A, B, C, 안, 녕, 1, 2, 3, ★, 
        System.out.println( java.util.Arrays.toString(chs) );
        // [A, B, C, 안, 녕, 1, 2, 3, ★]
    }
}

4 같이 보기

5 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}