如何把一段很长的String每隔一定字符就拆分

代码更新如下,分隔的效果更好了,而且割好之后可以直接粘贴使用

/**
 * 
 */
package com.zrar.main.tjfx.blh;

/*
 * Project:		税总12366
 * Author:		陈宜康
 * Company: 		杭州中软安人网络通信有限公司
 * Created Date:	2018-4-2
 * 
 * Copyright @ 2018 CS&S.COM – Confidential and Proprietary
 * 
 * History:
 * ------------------------------------------------------------------------------
 * Date			|time		|Author	|Change Description		*/

/**
 * 把一句很长的sql每隔80个字符拆分一次拆分成
 */
public class AutoPatch {
	public static void main(String[] args) {
		StringBuilder sql=new StringBuilder("");
		String sqlString=sql.toString().replace("\n", " ");
		for (int i = 1; i < 100; i++) {
			String shit="";
			for (int j = 0; j < i; j++) {
				shit+=" ";
			}
			sqlString=sqlString.replace(shit, " ");
		}
		String shit2=sqlString.toString();
		for (int i = 1; i < 100; i++) {
			String hehe="";
			for (int j = 0; j < i; j++) {
				hehe+=" ";
			}
			shit2=shit2.replace(hehe, " ");
		}
		System.out.println(makelinefeed(shit2));
	}
    public static String makelinefeed(String s) {
        String[] str = s.split(" ");
        StringBuffer buffer = new StringBuffer();
        int len = 0;
        for (int i = 0; i < str.length; i++) {
            len += str[i].length();
            if (len > 80) {
                buffer.append(" \"\n\" " + str[i] + " ");//利用StringBuffer对字符串进行修改
                len = str[i].length()+1;//+1为换行后读出空格一位
            } else {
                buffer.append(str[i] + " ");
                len++;
            }
        }
        return "\" "+buffer.toString()+" \"";
    }
}

你可能感兴趣的:(Java基础知识)