Board logo

標題: 203 拆解字串 [打印本頁]

作者: b790113g    時間: 2012-2-25 11:26     標題: 203 拆解字串

本帖最後由 b790113g 於 2012-2-25 11:59 編輯
) ?$ u2 I, |+ h$ N! z
" L$ s4 V7 C& z: ?8 z(1) 可判斷以逗號區隔之字串個數
* H1 K$ O1 i& Q: ]- G(2) 使用者輸入以逗號隔開不特定個數字串,程式將逗號兩旁之字擷取儲存,並分行顯示
6 L% p' {/ y  G8 F4 A' b/ k(3) 若輸入空白字元,執行結果顯示警告訊息
9 p/ U, d  l0 b6 ~4 \0 K. |( f3 N* W) e( L, P! Z* P
import java.util.*;
  1. import java.util.*; //引入才可使用split
  2. public class jva203{
  3.         public static void main(String arg[]){
  4.                 if(arg.length!=1){
  5.                         System.out.println("參數借誤!字串中不得有空白或必須以逗號隔開!");
  6.                         return ;
  7.                 }
  8.                 //111,2222,3333  split() 必須引入 java.util.*
  9.                 String str[] = arg[0].split(",");
  10.                 System.out.println("逗號隔開的字串個數共有: "+str.length);
  11.                
  12.                 for(int i=0;i<str.length;i++){
  13.                         System.out.println( (i+1)+"."+str[i] );
  14.                 }
  15.         }
  16. }
複製代碼

作者: TOM    時間: 2012-2-25 12:02

  1. import java.util.* ;
  2. public class jva203{
  3.         public static void main (String arg[]){
  4.          
  5.                 if(arg.length!=0)
  6.                 {
  7.                 System.out.println("參數借誤!字串中不得有空白或必須以逗號隔開!");
  8.                 return ;
  9.                 }
  10.                 String str[] = arg[0]spilt(",");
  11.                 System.out.println("逗號隔開的字串數共有: "+str.length);
  12.                 for(int i=0;i<str.length;i++)
  13.                 {
  14.                 System.out.println( (i+1)+"."+str[i]);
  15.                 }
  16.         }
  17. }
複製代碼

作者: lon    時間: 2012-2-25 12:12

  1. import java.util.*;
  2. public class jva202{
  3.         public static void main(String arg[]){
  4.         if(arg.length!=1){
  5.         System.out.println("參術借誤!字串中不得有空白或必須以逗號隔開!");
  6.         return;
  7.         }
  8.         String str[]=arg[0].split(",");
  9.         System.out.println("逗號隔開的字串個數共有:"+str.length);
  10.         for(int i=0;i<str.length;i++){
  11.         System.out.println((i+1)+"."+str[i]);
  12.         }
  13.         }
  14. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2