題目描述
•連續輸入字符串,請按長度爲8拆分每一個字符串後輸出到新的字符串數組;
•長度不是8整數倍的字符串請在後面補數字0,空字符串不處理。
輸入描述
連續輸入字符串(輸入2次,每一個字符串長度小於100)
輸出描述
輸出到長度爲8的新字符串數組
輸入例子
abc
123456789
輸出例子
abc00000
12345678
90000000
算法實現
import java.util.Scanner;
/**
*
* Declaration: All Rights Reserved !!!
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder builder = new StringBuilder(256);
while (scanner.hasNext()) {
builder.setLength(0);
String input = scanner.nextLine();
stringSplit(builder, input);
input = scanner.nextLine();
stringSplit(builder, input);
System.out.print(builder);
}
scanner.close();
}
private static void stringSplit(StringBuilder builder, String str) {
if (str == null || str.length() < 1) {
return;
}
int pos = 0;
while ((pos += 8) < str.length()) {
builder.append(str.substring(pos - 8, pos)).append("\n");
}
// 若是str.length() < pos,說明最後的不足8個字符或者恰好8個
if (str.length() <= pos) {
builder.append(str.substring(pos - 8, str.length()));
for (int i = str.length(); i < pos; i++) {
builder.append(0);
}
builder.append("\n");
}
}
}