Java批量将文件中的段落替换成空格,根据指定分隔符换行(SQL示例)
我的需求是SQL文件中有成千的类似数据,我要将它们进行转换格式,如下图
第一步:将字符段楼替换,使用word排版
点击全部替换,替换之后如下图,这时候它是一串很长的字符串
第二步:编写Java替换程序
public class Demo { public static void main(String[] args) { String s = "'133','153','180','181','189','177','130','131','132','155','156','145','185','186','176','134','135','136','137','138','139','150','151','152','158','159','182','183','184','157','187','188','147','178','170'"; String str[] = s.split(","); StringBuilder sb = new StringBuilder(); StringBuilder rs = new StringBuilder(); int num = 15; // 15是分割段 int yu = str.length % num; int a = 0; for (int i = 0; i < str.length; i++) { a++; sb.append(str[i] + ","); if (a == num) { a = 0; rs.append(sb + "\n"); sb = new StringBuilder(); } } sb = new StringBuilder(); for (int i = yu; i > 0; i--) { sb.append(str[str.length - i] + ","); } rs.append(sb); System.out.println(rs.substring(0, rs.length() - 1)); System.out.println("\n" + str.length + " : " + (rs.toString().split(",").length)); } }
第三步:将输出的字符替换SQL字符即可
比如下面效果
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。