题解 | #字符串分隔#

字符串分隔

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

import java.util.*;
/**
*   记录自己解题的心路历程,以便自己复盘
*/
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        //空字符直接返回,不做处理
        if (str.length() == 0) {
            System.out.println(str);
            return;
        }
        //字符串的长度小于8,在后面进行补0
        if (str.length() < 8) {
            while (true) {
                str = str + 0;
                if (str.length() == 8) {
                    System.out.println(str);
                    return;
                }
            }
        }
        //字符串的长度大于8,先进行对字符串进行长度为8进行打印、拆分
        if (str.length() > 8) {
            System.out.println(str.substring(0, 8));
            while (true) {
                //对字符串进行拆分
                str = str.substring(8);
                //对拆分后的字符串的长度判断是否小于8
                if (str.length() < 8) {
                    //如果字符串的长度等于0,说明刚好拆分完,直接返回
                    if(str.length() == 0){
                        return;
                    }

                    //否则就对字符串进行补0切割
                    str += "00000000";
                    str = str.substring(0, 8);
                    System.out.print(str);
                    return;
                }
                //对拆分后的字符串的长度判断是否大于等于8
                if (str.length() >= 8) {
                    System.out.println(str.substring(0, 8));
                }
            }
        }
        System.out.println(str);
        sc.close();
    }
}
全部评论

相关推荐

点赞 2 评论
分享
牛客网
牛客企业服务