java - after splitting a string, what is the first element in the array?

后端 未结 4 1460
执笔经年
执笔经年 2021-01-11 16:30

I was trying to split a string into an array of single letters. Here\'s what I did,

String str = \"abcddadfad\"; 
System.out.println(str.length());    //  ou         


        
4条回答
  •  [愿得一人]
    2021-01-11 17:21

    You can Also try this way

    String str = "abcddadfad";
    System.out.println(str.length());  // output: 10
    String[] strArr = new String[str.length()];
    for (int i = 0; i < strArr.length; i++) {   
    

    strArr[i] = "" + str.charAt(i);

        // As per  ratchet freak comment: it's easier (and more efficient) to use 
         strArr[i] = substring(i,i+1);
    }
    System.out.println(strArr.length); // output: 10
    System.out.println(strArr[0]);     // output: a
    

    As per

提交回复
热议问题