题解 | #字符串分隔#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); int strLength = str.length(); int n = strLength % 8; if (n != 0) { // 将字符串长度补足为 8 的倍数 for (int i = 0; i < 8 - n; i++) { str = str + "0"; } } strLength = str.length(); // 重新获取字符串的长度 int epoch = strLength / 8; for (int j = 0; j < epoch; j++) { // 输出子字符串 String subStr = str.substring(8*j, 8*j+8); System.out.println(subStr); } } }