「java」改行コードと余計スペースを削除するメモ
Javaコード
package com.changfa;
import java.util.regex.Pattern;
public class sqlspaceDemo {
public static void main(String[] args) {
String sql = “SELECT * FROM \n" +
" `sampledb`.`foo` LIMIT 0, 50″;
String s = “SELECT * FROM `sampledb`.`foo` LIMIT 0, 50″;
String sql2 = Pattern.compile(" {2,}").matcher(s).replaceAll(" “);
String sql3 = s.replaceAll(" {2,}"," “);
String sql4 = sql.replace('\r’, ' ').replace('\n’, ' ').replaceAll(" {2,}"," “);;
String sql5 = sql.replace('\r’, ' ').replace('\n’, ' ').replaceAll(" {2,}?"," “);;
String sql6 = sql.replace('\r’, ' ').replace('\n’, ' ').replaceAll(" {2,}+"," “);;
System.out.println(sql2);
System.out.println(sql3);
System.out.println(sql4);
System.out.println(sql5);
System.out.println(sql6);
}
}
実行結果
SELECT * FROM `sampledb`.`foo` LIMIT 0, 50
SELECT * FROM `sampledb`.`foo` LIMIT 0, 50
SELECT * FROM `sampledb`.`foo` LIMIT 0, 50
SELECT * FROM `sampledb`.`foo` LIMIT 0, 50
SELECT * FROM `sampledb`.`foo` LIMIT 0, 50