字符串分隔

•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

 

连续输入字符串(输入2次,每个字符串长度小于100)

输出到长度为8的新字符串数组

abc 123456789

abc00000 12345678 90000000

 1 import java.util.Scanner;
 2  
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         while(sc.hasNext()){
 7             String s1 = sc.nextLine();
 8             String s2 = sc.nextLine();
 9             getStr(s1);
10             getStr(s2);
11             }
12         }
13     public static void getStr(String s){
14         StringBuffer sb = new StringBuffer(s);           
15         if(s.length() % 8!= 0){//先末尾补0凑成8的倍数
16             int n = 8 - s.length() % 8;
17             for(int i = 0;i < n;i++){
18                 sb.append("0");
19             }
20         }
21         while(sb.length() >= 8){
22             System.out.println(sb.substring(0, 8));//取前8字串
23             sb = sb.delete(0, 8);//删除前8字串
24         }    
25     }
26 }

 

 有两个细节要注意 1.连续输入字符串(输入2次,每个字符串长度小于100) 

2.空字符串不处理????

posted @ 2016-08-19 14:58  蛋蛋的守护  阅读(183)  评论(0)    收藏  举报