题目
连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
思路
非8倍长度,先补0,然后使用substring切割
代码
链接:https://www.nowcoder.com/questionTerminal/d9162298cb5a437aad722fccccaae8a7
来源:牛客网
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String s = new String(sc.nextLine());
if(s.length()%8 !=0 )
s = s + "00000000";
while(s.length()>=8){
System.out.println(s.substring(0, 8));
s = s.substring(8);
}
}
}
}