版权声明:博客内容为本人自己所写,请勿转载。 https://blog.csdn.net/weixin_42805929/article/details/82720530
字符串分隔
题目描述
•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入描述:
连续输入字符串(输入2次,每个字符串长度小于100)
输出描述:
输出到长度为8的新字符串数组
示例1
输入
abc
123456789
输出
abc00000
12345678
90000000
代码1:
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String st = sc.nextLine();
divide(st);
}
}
public static void divide(String st){
while(st.length() >= 8){
System.out.println(st.substring(0,8));//输出下标为0到8的字符串
st = st.substring(8);//去掉原来的字符串的前8个字符
}
if(st.length() < 8 && st.length() > 0){
st = st + "00000000";
System.out.println(st.substring(0,8));
}
}
}
代码2:
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = "";
while(sc.hasNext()){
String st = sc.nextLine();
if(st.length()%8 != 0){
st = st + "00000000";
}
while(st.length() >= 8){
System.out.println(st.substring(0,8));
st = st.substring(8);
}
}
}
}