题解 | 字符串分隔
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String input = in.nextLine();
for (int i = 0; i < input.length(); i += 8) {
String group = input.substring(i, Math.min(i + 8, input.length()));
// 不足8位补零
System.out.println(String.format("%-8s", group).replace(' ', '0'));
}
}
}
直接按8位截取,根据正则表达式替换,不足8位的补0;

