字符串截取

字符串分隔

http://www.nowcoder.com/questionTerminal/d9162298cb5a437aad722fccccaae8a7

思路

  1. 需要输入字符串,用到Scanner和hasNext()。
    (1)建立 Scanner sc = new Scanner(System.in);
    (2)判断有无输入用sc.hasNext().接收字符串使用sc.nextLine().
  2. 一次性接受全部的字符串,对8取余,获知需要补0的位数。使用StringBuilder中的append()函数进行字符串修改,别忘了toString()。
    字符串缓冲区的建立:StringBuilder sb = new StringBuilder();
  3. 输出时,截取前8位进行输出,并更新字符串。用到str.substring()函数:
    (1)str.substring(i)意为截取从字符索引第i位到末尾的字符串。
    (2)str.substring(i,j)意为截取索引第i位到第(j-1)位字符串。包含i,不包含j。

代码

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()){
            String str = sc.nextLine();
            StringBuilder sb = new StringBuilder();//牢记字符串缓冲区的建立语法
            sb.append(str);//字符串缓冲区的加入
            int size = str.length();
            int addZero = 8 - size%8;//addzero的可能值包括8
            while((addZero > 0)&&(addZero<8)){//注意边界调节,避免addzero=8
                sb.append("0");//使用‘’或“”都可
                addZero--;
            }
            String str1 = sb.toString();
            while(str1.length()>0){
                System.out.println(str1.substring(0,8));
                str1 = str1.substring(8);
            }

        }
    }
}

全部评论
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String str = sc.nextLine(); p(str); } } public static void p(String str){ if(str.length()>8){ System.out.println(str.substring(0,8)); p(str.substring(8,str.length())); }else{ StringBuffer strb = new StringBuffer(str); for(int i=str.length();i<8;i++){ strb.append("0"); } System.out.println(strb); } } }
1 回复 分享
发布于 2021-12-09 20:02
提交是通过的
1 回复 分享
发布于 2020-08-18 09:39
个人觉得第8行的StringBulider创建应该放在while循环前,不应该放在循环里面,因为循环会创建多个sb这就不对了;另外16行在转换为字符串时sb是全局变量,在循环里则是局部变量,会报错
5 回复 分享
发布于 2020-10-05 23:24
现在这题不知道那个活雷锋提供了一个错的测试用例,你的代码通不过了。
4 回复 分享
发布于 2020-11-13 01:28
两个if条件,填充8个0,然后substring截取,哈哈哈哈哈
2 回复 分享
发布于 2022-08-29 23:01 贵州
最后截取的循环中,会产生大量的字符串对象,性能不太好
1 回复 分享
发布于 2022-06-16 22:33
这个不行的,当输入的字符串比较长的时候就不行了,他只是把第一个八个字符剪切出来了,后面的全部连在一起
1 回复 分享
发布于 2022-05-12 18:14
Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String s = in.nextLine(); StringBuilder sb = new StringBuilder(); sb.append(s); // 补0 int k; while ((k = sb.length()) % 8 != 0) { sb.append("0"); } // 打印 int i = 0; int j = 8; while (j <= sb.length()) { System.out.println(sb.substring(i, j)); i = j; j += 8; }
点赞 回复 分享
发布于 2024-12-08 11:55 福建
Scanner in = new Scanner(System.in); //注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { String s = in.nextLine(); while(s.length()>0){ if(s.length()>=8){ System.out.print(s.substring(0,8)+"\n"); s = s.substring(8); }else { s = s+"0"; } } }
点赞 回复 分享
发布于 2024-08-22 13:10 北京
第一眼看就不对啊,不看 size 跟 8 谁大就取摸 。 这个解答只适用于输入字符串小于 8
点赞 回复 分享
发布于 2024-04-16 16:56 北京
肯定有问题啊,空字符串都没处理,为啥提交能过,这啥破用例
点赞 回复 分享
发布于 2023-08-22 10:23 广东
性能貌似很差。但是也算是做出来了。
点赞 回复 分享
发布于 2023-04-18 11:35 香港
可以
点赞 回复 分享
发布于 2022-06-02 18:32
试试这个: import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int minStrLen = str.length()%8; for (int i=0; i<(str.length()-minStrLen)/8; i++){ System.out.println(str.substring(i*8,i*8+8)); } System.out.print(str.substring(str.length()-minStrLen, str.length())); for (int i=0; i<(8-minStrLen)%8; i++) { System.out.print("0"); } } }
点赞 回复 分享
发布于 2022-04-21 21:28
题目说的“空字符串不处理”啥意思,空还是空格,我还以为不能把输入的空格append上
点赞 回复 分享
发布于 2022-03-27 22:36
例子中说的是按行分割,不能一次性全部把全部字符串拼接在一起
点赞 回复 分享
发布于 2022-02-21 16:03
import java.util.*; /** * @author zhhy * @date :Created in 2022/1/5 23:07 * @description: * @modified By:2022/1/5 23:07 */ public class Main { public static void main(String[] args){ // new HJ1().method(); // new HJ2().method(); // new HJ3().method(); new HJ4().method(); } } class HJ4{ public void method(){ Scanner scanner = new Scanner(System.in); List<string> list = new ArrayList<>(); while (scanner.hasNext()){ String l = scanner.nextLine(); hh(list,l); } list.forEach(s -> System.out.println(s)); } public void hh(List<string> list, String l){ if(l.length() == 8){ list.add(l); }else if(l.length() < 8){ list.add(bu0(l, 8-l.length())); }else{ list.add(l.substring(0,8)); hh(list,l.substring(8)); } } private String bu0(String l, int n){ StringBuffer sb = new StringBuffer(); sb.append(l); for(int i=0; i</string></string>
点赞 回复 分享
发布于 2022-01-08 16:49
while那里的hasNext(),在本地my eclips上跑会不断提示输入,网页版提交就没问题,有大神能讲一下吗
点赞 回复 分享
发布于 2021-03-21 16:45
题目要求输出为数组
点赞 回复 分享
发布于 2020-04-08 19:50

相关推荐

昨天 20:41
已编辑
北京交通大学 算法工程师
字节跳动 训练框架研发 (N+2) * (12 + 3) 硕士211
点赞 评论 收藏
分享
评论
264
16
分享

创作者周榜

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