字符串分割
Posted gy7777777
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串分割相关的知识,希望对你有一定的参考价值。
先将字符串补成8的倍数,然后再分割字符串,打印输出
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
//我也不知道为什么要用while循环,没有循环牛客就会报错,说没有循环输入 while(sc.hasNext()) { int n = sc.nextInt(); for(int i=0; i<n; i++) { String temp = sc.next(); while(temp.length()%8!=0) { temp += "0"; } for(int j=0; j<temp.length(); j+=8) { System.out.println(temp.substring(j, j+8)); } } } } }
顺便记录一下我写的跟屎一样的玩意
import java.util.*; import java.lang.*; public class Main{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String num = sc.nextLine(); int n = Integer.valueOf(num); String[] str = new String[n]; for (int i = 0; i < n; i++) { str[i] = sc.nextLine(); if (str[i].length() < 8) { str[i] = ArrCmp(str[i]); System.out.println(str[i]); } else if (str[i].length() > 8) { int count = str[i].length() / 8; int r = str[i].length() % 8; if (r == 0) { show(str[i], count); } else { String pre = str[i].substring(0, count * 8); show(pre, count); String sub = str[i].substring(count * 8); sub = ArrCmp(sub); System.out.println(sub); } } else { System.out.println(str[i]); } } } } public static String ArrCmp(String str) { char[] ss = str.toCharArray(); char[] res = new char[8]; int len = str.length(); for (int i = 0; i < len; i++) { res[i] = ss[i]; } for (int i = len; i < 8; i++) { res[i] = ‘0‘; } return new String(res); } public static void show(String str, int count) { char[] ss = str.toCharArray(); char[] res = new char[8]; for (int i = 0; i < count; i++) { for (int j = 0; j < 8; j++) { res[j] = ss[i * 8 + j]; } System.out.println(new String(res)); } } }
以上是关于字符串分割的主要内容,如果未能解决你的问题,请参考以下文章