返回列表 發帖

203 拆解字串

本帖最後由 b790113g 於 2012-2-25 11:59 編輯 3 N+ S, P! c/ G

" y& g: e  ^1 q0 f% w/ O# k(1) 可判斷以逗號區隔之字串個數! B. |/ L/ @2 i) P. I
(2) 使用者輸入以逗號隔開不特定個數字串,程式將逗號兩旁之字擷取儲存,並分行顯示9 `1 {" ?) b' m$ O. [
(3) 若輸入空白字元,執行結果顯示警告訊息. ^* L& O6 `4 T1 u6 x  [' K
2 k; Y5 g, f0 H( P# D; N" w
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. }
複製代碼

  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. }
複製代碼
水桶小鄭,鯰魚

TOP

  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. }
複製代碼
陳彥綸

TOP

返回列表