题解 | #字符串分隔#

字符串分隔

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String input = in.nextLine();
            // 如果输入为空字符串,则不处理
            if (input.isEmpty()) {
                continue;
            }
            // 分割字符串,每8个字符为一组
            for (int i = 0; i < input.length(); i += 8) {
                int endIndex = Math.min(i + 8, input.length());
                String subString = input.substring(i, endIndex);
                // 如果子字符串长度不足8,则在末尾补0
                while (subString.length() < 8) {
                    subString += "0";
                }
                System.out.println(subString);
            }
        }


    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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