怎样用java将一段字符串每隔7个字符输出一次???用system.ou.println( )输出

2024-12-01 16:55:01
推荐回答(2个)
回答1:

public static void main(String[] args) {
String str = "asfdas7fsafds7afsadf7dsafsa7dfdsaf7a";

if(str.length()>7){
String temp = str.substring(0, 7);
while(str.length()>7){
System.out.println(temp);
str = str.substring(7, str.length());
if(str.length() > 7){
temp = str.substring(0, 7);
}else{
temp = str;
}
}
System.out.println(temp);
}else{
System.out.println(str);
}
}

回答2:

建议再使用StringBuilder来处理一下