题解 | #字符串分隔&填充#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
import java.util.Scanner; // 字符串右填充:String.format("%1$-" + 8 + "s", str).replaceAll(" ", "0");//总长度8,原字符串长度不足8空格自动补全 // 字符串左填充:String.format("%" + 8 + "s", str).replaceAll(" ", "0");//总长度8,原字符串长度不足8空格自动补全 //字符串分隔,substring函数参数含义:str.subString(start,endPosition) public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); int len = str.length(); int yushu = len % 8 ; int count = yushu > 0 ? (len / 8 + 1) : len / 8; String[] arr = new String[count]; if (len < 8) { arr[0] = String.format("%1$-" + 8 + "s", str).replaceAll(" ", "0"); System.out.println( arr[0]); return; } for (int i = 0; i < count; i++) { if (yushu > 0 && i == count - 1) { arr[i] = String.format("%1$-" + 8 + "s", str.substring(8 * i, len)).replaceAll(" ", "0"); break; } arr[i] = str.substring(8 * i, 8 * i + 8); } for (int i = 0; i < count; i++) { System.out.println( arr[i]); } } }#字符串填充#