题解 | #字符串分隔#--利用字符串截取和格式化方法

字符串分隔

https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str = in.nextLine();
            int len = str.length();
            // 长度小于8直接右补0,利用String.format("%0numd", 0)
            if (len < 8) {
                System.out.println(str + String.format("%0" + (8 - str.length()) + "d", 0));
            } else {
                // 确定有多少个8长度的字符串
                int num = len / 8;
                // 最后一个字符串的长度
                int lastStrNum = len % 8;
                // 每8位截取一次
                for (int i = 0; i < num; i++) {
                    System.out.println(str.substring(8 * i, 8 * (i + 1)));
                }
                if (lastStrNum > 0) {
                    System.out.println(str.substring(len - lastStrNum) + String.format("%0" + (8 - lastStrNum) + "d", 0));
                }
            }
        }
    }
}

全部评论

相关推荐

2025-12-22 16:31
已编辑
桂林电子科技大学 Python
很奥的前端仔:如果你接了offer 临时又说不去 hr确实要多做一些工作。 当然如果是接offer之前当我没说
点赞 评论 收藏
分享
01-14 12:34
门头沟学院 C++
牛马人的牛马人生:太暖心了啊 配环境是真烦
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务