JavaWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JavaWorld@TW » Java 新手區 » String  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 請問要怎樣使用兩個split[]
chuang5089





發文: 232
積分: 0
於 2006-04-06 00:50 user profilesend a private message to usersend email to chuang5089reply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問要怎樣使用兩個split[]要是我的一串string裡有兩個variable 要使用split 來切斷字元
要怎樣改是不是我的寫法不對

1
2
3
4
5
6
7
8
9
10
11
12
 public static void main(String[] args) {
    String str = "aa!bb!cc~ab!bc!cd";
    String strs[]= str.split("~");
    String strE[]= str.split("!");
    int count =0;
    
    for(int j=0; j < strs.length; j++){
      for (int i = 0; i < strE.length; i++){    
          System.out.println(count + " " + strE[i]);           
      } 
    count++;
   }


我想要的結果是這樣
0 aa
0 bb
0 cc
1 ab
1 bc
1 cd


vote up 0 vote down
reply to postreply to post
http://blog.yam.com/javanull/
作者 Re:請問要怎樣使用兩個split[] [Re:chuang5089]
chuang5089





發文: 232
積分: 0
於 2006-04-06 01:35 user profilesend a private message to usersend email to chuang5089reply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
可以使用這個方式但是要怎樣變成這樣
1 aa
2 bb
3 cc
1 ab
2 bc
3 cd

1
2
3
4
5
6
7
8
9
10
11
12
 public static void main(String[] args) {
      String str = "aa!bb!cc~ab!bc!cd";
      String strs[]= str.split("!");
      String strE[]= str.split("[~!]");
      int count =strs.length;
      
        for (int i = 0; i < strE.length; i++){
            System.out.println(count + " " + strE[i]);
        }                
        
    }//end main
 


vote up 0 vote down
reply to postreply to post
http://blog.yam.com/javanull/
作者 Re:請問要怎樣使用兩個split[] [Re:chuang5089]
kebin_liu

沿著地表飛行

版主

發文: 1812
積分: 11
於 2006-04-06 08:34 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
     public static void main(String[] args) {
      String str = "aa!bb!cc~ab!bc!cd";
      String strs[]= str.split("~");
      for(int i=0;i<strs.length;i++){
      String strE[]= strs[i].split("!");
        for (int j = 0; j < strE.length; j++){
            //這行 會變成 0 aa, 0 bb, 0 cc,1 ab, 1 bc, 1 cd
            System.out.println(i + " " + strE[j]);
            //這行 會變成 1 aa, 2 bb, 3 cc,1 ab, 2 bc, 3 cd
            System.out.println((j+1) + " " + strE[j]);
        }  
      }
    }


vote up 0 vote down
reply to postreply to post
作者 Re:請問要怎樣使用兩個split[] [Re:kebin_liu]
GERRYccc





發文: 28
積分: 0
於 2006-04-20 16:13 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
更正於下。

vote up 0 vote down
GERRYccc edited on 2006-04-20 23:49
reply to postreply to post
作者 Re:請問要怎樣使用兩個split[] [Re:GERRYccc]
kebin_liu

沿著地表飛行

版主

發文: 1812
積分: 11
於 2006-04-20 19:03 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
GERRYccc wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static void main(String[] args) {
      
   String str = "aa!bb!cc~ab!bc!cd";
   String strs[]= str.split("~");
   String strE[]= str.split(" {0,}[~!] {0,}");
   //上面這句可同時切~、!  我也不知道為什麼,抄來的,知道的也請幫我解釋,謝謝。
 
   for(int j=0; j < strs.length; j++){
      for (int i = 0; i < strE.length/strs.length; i++){
         //把 j 拿來當作 count
         System.out.println(j + " " + strE[j*(strE.length/strs.length)+i]);
      }
   }//end outFor
}//end main

根據你的題目、問題得以上解。
小弟土法煉鋼,請多指教^^"


試試看 "aa!bb!cc~ab!bc!cd!de" 你的程式得不到 de。
另外,要同時切~! ,用"[~!]" or "~|!"也就可以了。


vote up 0 vote down
reply to postreply to post
作者 Re:請問要怎樣使用兩個split[] [Re:chuang5089]
GERRYccc





發文: 28
積分: 0
於 2006-04-20 23:54 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    String str = "aa!bb!cc~ab!bc!cd!de!ef!fg~12!23!34!45";
    String strS[]= str.split("~");
    String strE[]= str.split("[~!]");
    int sL=strS.length;
    int eL=strE.length;
    int strsL[]=new int[sL];
 
    for(int k=0;k<sL;k++){
      strsL[k]=strS[k].split("!").length;  //得到 ~ 隔開的部分各別長度。
    }
    int count=0;
    for (int i = 0; i < sL; i++){
      for(int j=0;j<strsL[i];j++){  //使用剛剛得到的各長度控制輸出個數
        System.out.println(i + " "+strE[count]);
        count++;  //總輸出累積
      }
    }


小弟竄改了題目,已求更完整達到需求。
也是土法煉鋼一則,請各位高手不吝繼續指教。謝謝^^"

另外想請問的是,split(" {0,}[~!] {0,}"); 其中 {0,} 是代表什麼意思呢?
用途是什麼?謝謝呢^^"


vote up 0 vote down
GERRYccc edited on 2006-04-21 00:00
reply to postreply to post

» JavaWorld@TW »  Java 新手區 » String

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JavaWorld@TW


Powered by Powerful JuteForum® Version Jute 1.5.8
Copyright© 2002-2003 Rainman Zhu,Zua,Netboy,Scott. All Rights Reserved.