例如有一串字符串:123456123456123456123456123456123456123456123456123456123456(字符串位数不确定)
我要的结果:分解成6个一组,直到没有字符串可以分解了。
123456
123456
123456
123456
123456
123456请各位大哥大姐,赐代码。
我要的结果:分解成6个一组,直到没有字符串可以分解了。
123456
123456
123456
123456
123456
123456请各位大哥大姐,赐代码。
调试欢乐多
String str = "123456123456123456123456123456123456123456123456123456123456" char[] c = str.toArray();//方法不记得鸟
StringBuffer sb = null
int num = 1;
for(int i = 0;i<c.length;i++){
if(num == 1){
sb = new StringBuffer();
sb.append(c[i]);
}
if(num == 6){
System.out.println(sb);
num = 0;
}
num++;
}
int k = 0;
for(int i = cs.length;i-=6;i>=6) {String tempS = new String(cs,k,i >= 6 ? k + 6, k + i);
//处理你的tempS
k += k;//往前步进
}
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abcddddddffefadfdd";
String[] arr = splitStringBySix(str);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
} } public static String[] splitStringBySix(String str) {
if (null == str || "".equals(str)) {
return null;
}
// 计算存放数据的数组的长度
int arrLength = (str.length() % 6 == 0) ? str.length() / 6 : str
.length() / 6 + 1;
String[] arr = new String[arrLength]; for (int i = 0; i < arr.length; i++) {
// 主要是处理,最后的几位不够6位的情况
if (i == arr.length - 1) {
arr[i] = str.substring(i * 6);
}
arr[i] = str.substring(i * 6, (i + 1) * 6);
}
return arr;
}
}
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abcddddddffefadfd";
String[] arr = splitStringBySix(str);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
} } public static String[] splitStringBySix(String str) {
if (null == str || "".equals(str)) {
return null;
}
// 计算存放数据的数组的长度
int arrLength = (str.length() % 6 == 0) ? str.length() / 6 : str
.length() / 6 + 1;
String[] arr = new String[arrLength]; for (int i = 0; i < arr.length; i++) {
// 主要是处理,最后的几位不够6位的情况
if (i == arr.length - 1) {
arr[i] = str.substring(i * 6);
}else{
arr[i] = str.substring(i * 6, (i + 1) * 6);
}
}
return arr;
}
}
for (int i=0; i<s.length()/6; i++)
System.out.println(s.substring(i*6, i*6+6));
public class Test {
public static void main(String[] args) {
String str = "123456123456123456123456123456123456123456123456123456123456123";
for (int i = 0; i < str.length(); i += 6) {
System.out.println(str.substring(i, i + 6 > str.length() ? str.length() : i + 6));
}
}
}
public class Test {
public static void main(String[] args) {
String str = "123456123456123456123456123456123456123456123456123456123456123";
for(;str.length()>=6; str=str.substring(6)) {
System.out.println(str.substring(0, 6));
}
if (str.length()>0) {System.out.println(str);}
}
}
嘿嘿,楼主说6个一组嘛,并没指出最后不够6个的要还是不要。
如果不要,上面的版本就是唯一正确的。
如果最后不够6个的仍然要,也只要稍加调整,行数不变for (i=0; i<=s.length()/6; i++)
System.out.println(s.substring(i*6, Math.min(i*6+6,s.length())));
package com.test;public class reallytest { /**
* @param args
*/
public static void main(String[] args) {
String s = "12312231321316546464613215674879";
if(s.length()%6==0){
for (int i=0; i<s.length()/6; i++)
System.out.println(s.substring(i*6, i*6+6));
}else{
for (int i=0; i<s.length()/6; i++)
System.out.println(s.substring(i*6, i*6+6));
System.out.println(s.substring(s.length()/6*6));
}
}
}
* @param args
*/
public static void main(String[] args) {
String s = "12312231321316546464613215674879";
if(s.length()%6==0){
for (int i=0; i<s.length()/6; i++)
System.out.println(s.substring(i*6, i*6+6));
}else{
for (int i=0; i<s.length()/6; i++)
System.out.println(s.substring(i*6, i*6+6));
System.out.println(s.substring(s.length()/6*6));
}
}
}
已通过
String s = "123456123456123456123456123456123456123456123456123456123456";
String [] ss = s.split("1");
for(int i = 1; i<ss.length; i++){
System.out.println("1"+ss[i]);
}
}
System.out.println(s.substring(i*6, Math.min(i*6+6,s.length())));for (int i=0; i<s.length()/6; i++)
System.out.println(s.substring(i*6, i*6+6));这两个方案都符合需求,不过我已用另一种方法了。杯具
public static boolean isNotboolean(StringBuffer str){
List<String> strList = new ArrayList<String>();
String tempList="";
boolean bool = false;
int start = 0;
int end = start + 6;
while (true) {
if (start >= str.length())
break;
String temp = str.substring(start, end);
strList.add(temp);
System.out.println(temp);
start = end;
end = end + 6;
if (end >= str.length()) {
end = str.length();
}
}
for (int i = 0; i < strList.size(); i++) {
tempList=strList.get(i).toString();
System.out.println(tempList+"----");
if("100202".equals(tempList) || "100203".equals(tempList) || "100401".equals(tempList) ||
"100402".equals(tempList) || "100302".equals(tempList) || "100303".equals(tempList) ||
"300202".equals(tempList) || "300203".equals(tempList) || "300301".equals(tempList) ||
"300302".equals(tempList) || "300303".equals(tempList) || "300304".equals(tempList) ||
"100110".equals(tempList) || "100308".equals(tempList)){
bool=true;
}
}
if(bool){
return bool;
}
return false;
}
}