代码如下:
public class Test {
public static void main(String[] args){
String s="12345543211234554321";
StringBuffer s1=new StringBuffer(s);
int index;
for(index=5;index s1.insert(index, '\n'); } System.out.println(s+"每隔5个字符换行:"); System.out.println(s1); } }
public class test{
public static void main(String args[]){
String str="我很想将一段字符串从第一个字符开始往后数,每隔五个就在后面加上换行符,请各位java高手帮忙,谢谢" ;
StringBuffer s = new StringBuffer(str);
for(int index = 0; index < s.length();index++){
if(index%6==0){
s.insert(index,"\n");
}
}
System.out.println("After insert:"+s.toString());
}
}
Windows下要用\r\n,以下是代码
--------------------------------------------------------
String str="我很想将一段字符串从第一个字符开始往后数,每隔五个就在后面加上换行符,请各位java高手帮忙,谢谢";
String str2 = "";
for(int i = 0; i < str.length(); i += 5)
if(i + 5 < str.length())
str2 += str.substring(i, i + 5) + "\r\n";
else
str2 += str.substring(i) + "\r\n";
windows下的\n换行在showMessageDialog中好像不管用吧