public class Forest {
public static void main(String[] args){
String test = "This is a test";
String[] tokens = test.split("\s");
System.out.println(tokens.length);
}
}

----------------------

解析:

spilt("\s")產生錯誤

Invalid escape sequence (valid ones are  \b  \t  \n  \f  \r  \"  \'  \\ )

要正確要改成 test.split("s"); 或 test.split("\\s");

主要是看是否要用跳脫字元

----------------------

 test.split("s"); 用s來分,會分成4塊

ANS:4

----------------------

test.split("\\s"); 用\s來分(空格),會分成4塊

ANS:4

arrow
arrow
    全站熱搜

    pcwiki 發表在 痞客邦 留言(0) 人氣()