• 字符串分隔


    题目描述

    •连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
    •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

    输入例子:
    abc
    123456789
    输出例子:
    abc00000
    12345678
    90000000

     1 import java.util.Scanner;
     2 
     3 public class Test1 {
     4 
     5     /**
     6      * @param args
     7      */
     8     public static void main(String[] args) {
     9         Scanner sc = new Scanner(System.in);
    10         String s1 = sc.nextLine();
    11         int len1 = s1.length();
    12         StringBuffer sb1 = new StringBuffer(s1);
    13         String s2 = sc.nextLine();
    14         int len2 = s2.length();
    15         StringBuffer sb2 = new StringBuffer(s2);
    16         partStr(sb1, len1);
    17         partStr(sb2, len2);
    18 
    19     }
    20 
    21     public static void partStr(StringBuffer sb, int len) {
    22         if (len < 8) {
    23             for (int i = len; i < 8; i++) {
    24                 sb.append('0');
    25             }
    26             System.out.println(sb);
    27         }
    28         if (len % 8 == 0) {
    29             for (int i = 0; i < len / 8; i++) {
    30                 String temp = sb.substring(0 + 8 * i, 8 + 8 * i);
    31                 System.out.println(temp);
    32             }
    33         }
    34         if (len > 8 && (len % 8 != 0)) {
    35             int x = len % 8;
    36             for (int i = 0; i < 8 - x; i++) {
    37                 sb.append('0');
    38             }
    39             int lenTemp = sb.length();
    40             for (int i = 0; i < lenTemp / 8; i++) {
    41                 String temp = sb.substring(0 + 8 * i, 8 + 8 * i);
    42                 System.out.println(temp);
    43             }
    44 
    45         }
    46     }
    47 }

     另一种参考网友代码:

     1 import java.util.Scanner;
     2 
     3 public class Temp {
     4 
     5     public static void main(String[] args) {
     6 
     7         // TODO Auto-generated method stub
     8 
     9         Scanner sc = new Scanner(System.in);
    10 
    11         String str1 = sc.next();
    12 
    13         String str2 = sc.next();
    14 
    15         fenge(str1);
    16 
    17         fenge(str2);
    18 
    19         sc.close();
    20 
    21     }
    22 
    23     public static void fenge(String string) {
    24 
    25         StringBuilder str = new StringBuilder(string);
    26 
    27         int length = str.length();
    28 
    29         while (length % 8 > 0) {
    30 
    31             int index = length % 8;
    32 
    33             str.append("0");
    34 
    35             length = str.length();
    36 
    37         }
    38 
    39         int length1 = str.length();
    40 
    41         for (int i = 0; i < length1; i = i + 8) {
    42 
    43             String str1 = str.substring(i, i + 8);
    44 
    45             System.out.println(str1);
    46 
    47         }
    48 
    49     }
    50 
    51 }
  • 相关阅读:
    ansible源码解读
    python标准模块(下)
    python学习之算法、自定义模块、系统标准模块(上)
    pathon 基础学习-集合(set),单双队列,深浅copy,内置函数
    python的map,filter,reduce学习
    python 列表
    python生成器、装饰器、正则
    python 模块学习
    python基础学习(一)--数据类型
    时间复杂度的计算
  • 原文地址:https://www.cnblogs.com/crazybuddy/p/5309899.html
Copyright © 2020-2023  润新知