String str = "aaaa bbb  bb   ccc   aadf";如何将以上字符串分解为如下形式str1 = aaaa;
str2 = bbb;
str3 = bb;
str4 = ccc;
str5 = aadf;每段字符之间得空个数是不一定的

解决方案 »

  1.   

    方案一:
    split
    public String[] split(String regex)
    Splits this string around matches of the given regular expression. 
    This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result 
    : { "boo", "and", "foo" } 
    o { "b", "", ":and:f" } 
    Parameters:
    regex - the delimiting regular expression 
    Returns:
    the array of strings computed by splitting this string around matches of the given regular expression 
    Throws: 
    PatternSyntaxException - if the regular expression's syntax is invalid
    Since: 
    1.4 
    See Also:
    Pattern方案二:
    只考虑空格所字符串拆开,然后对每个子串调用.trim()方法去掉子串前后的空格。
      

  2.   

    用StringTokenizer 简单得很 new StringTokenizer(你的字符串 , 分隔符 , false)
      

  3.   

    正则式完全可以取代StringTokenizer,所以没必要用StringTokenizerString ss[] = str.split(" +");
      

  4.   

    这也算问题,split
    StringTokenier功能有限!
      

  5.   

    对不起,楼上的各位大哥,因为LZ说的是字符串中间的空格不一定,所以用split(" ")分开的结果还需要处理。下面小弟的程序是正确的,大家有什么问题可以讨论[email protected]
    public class test{
    public static void main(String[] args){
    String str="aa bb  ccc   ddd" ;
    String[] string = str.split(" ");
    for(int i=0;i<string.length;i++){
    if(!string[i].equals("")){
    System.out.println(string[i].trim());
    }
    }
    }
    }
      

  6.   

    对不起,楼上的各位大哥,因为LZ说的是字符串中间的空格不一定,所以用split(" ")分开的结果还需要处理。下面小弟的程序是正确的,大家有什么问题可以讨论[email protected]
    没人说用的是split(" ")么
      

  7.   

    用string的indexof(char)和substring(int i,int j)方法去截取就行了,我昨晚就搞成功了一个~~~
      

  8.   

    String str = " aaaa bbb  bb   ccc    aadf";
            
            while (str.indexOf("  ")>0){
                str = str.replaceAll("  "," ");
            }
            
            String[] ss = str.trim().split(" ");
            
            for (int i=0;i<ss.length;i++) System.out.println(ss[i]);
      

  9.   

    public class StringSplit {
    public static void main(String[] args) {
       String s = "aaa bbb  ccc    ddd      eee ";
       String []ss = s.split(" +");
       for(int i = 0;i<ss.length;i++)
       System.out.println(ss[i]);
    }
    }
    /*aaa
    bbb
    ccc
    ddd
    eee*/
      

  10.   

    呵呵,挺有意思的。那大家在看看这个字符串吧
    String str = "   aa     bbb  ccc    ddd      e    ee ";
    有的运行结果是:aa
    bbb
    ccc
    ddd
    e
    ee
    Press any key to continue...
    //注意哦,aa上面还是有空行的,而不是的敲的回车
    我的运行结果是:
    aa
    bbb
    ccc
    ddd
    e
    ee
    Press any key to continue...
    没有空行的哦。
    希望大家继续讨论有时间加我聊哦[email protected]
      

  11.   

    split("空格+")最好
    至于楼上说的问题是因为split只考虑分隔符在中间的情况,如果是以分隔符开始的,就会出现数组第一个元素为空的现象。可以先用trim()切一下。
      

  12.   

    正则式 String ss[] = str.split(" +");
      

  13.   

    为什么我没分?
    我的split(\\s+)不正确么?