String test = "This is a test";  
String[] tokens = test.split("\s");  
System.out.println(tokens.length);  String[] tokens = test.split("\s");  这句没看明白什么意思?
这段程序要实现什么功能?

解决方案 »

  1.   

    将test字符串转换成字符串数组,分割符为空白
    tokens的四个元素就是"This", "is", "a", "test"貌似应该是
    String[] tokens = test.split("\\s");
      

  2.   

    楼主有问题吧
    编译过不了吧test.split("\\s");
    split这里是按空白(空格,制表符等)分割 字符串
    得到的数组有四个值:This,is,a,test;
      

  3.   

    程序是错的,是SCJP的一道题!我就是没看明白他这个split()方法是做什么的
      

  4.   


    //楼主的代码应该是下面这样,split接受一个正则表达式的参数 ,\s代表一个空白字符,在用的时候要多加一个\,要不然\s会被认为转义了
    String test = "This is a test";  
    String[] tokens = test.split("\\s");  
    System.out.println(tokens.length);
      

  5.   

    s 估计是space的缩写。 
      

  6.   

    String类型的split(参数)方法:按照参数分割字串。返回数组。。
      

  7.   

    正则表达式
    test.split("\\s"); 
      

  8.   

    String[] tokens = test.split("\\s");//把test字符串以空格分隔成多个字符串放入tokens
    结果应该是4
      

  9.   

     test.split("\\s"); 就是 test.split(" ");
      

  10.   

    今天去websense的笔试
    第一个题就是这个题,看来我对java语言细节的掌握还是不够。
    有很多情况我认为不用记住  到时候查API就行了。
      

  11.   

    应该是test.split("\\s")吧,表于以空格分割字符串
    split是以正则表达式分割字符串,
      

  12.   

    public class Test {  public static void main(String[] args) {
          
     String str = "a b c d e f g ";
     String[] array = str.trim().split("\\s");
     
     System.out.println("array的长度是: "+array.length);
     for(int i=0;i<array.length;i++){
     System.out.println(array[i]);
     }
     String str2 = "aa bb  cc   dd      ee         ff     gg ";
     String[] array2 = str2.trim().split("\\s+");
     
     System.out.println("array2的长度是: "+array2.length);
     for(int i=0;i<array2.length;i++){
     System.out.println(array2[i]);
     }
    }
    }