华为——字符串分隔

来源:互联网 发布:淘宝学校哪家好 编辑:程序博客网 时间:2024/05/21 19:28

题目描述

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


输入描述:

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



输出描述:

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

示例1

输入

abc123456789

输出

abc000001234567890000000
注意尽量使用StringBuffer减少内存开销:
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);while(sc.hasNext()){String str = sc.nextLine();while(str.length()>8){ String str2 = str.substring(0,8); str = str.substring(8, str.length()); System.out.println(str2);}if(str.length()<8){int index = 8-str.length();StringBuffer sb = new StringBuffer(str);for(int i =1;i<=index;i++){sb.append("0");}str = sb.toString();}System.out.println(str);}}}


原创粉丝点击