简化文字串
* String TrimSentence( String src )
将原串src中的所有多余空白符(空格,tab,回车等)去掉后,结果就在目标串dst中;引号中的空白符不能去掉,并要考虑引号中的转义字符;两个单词数字之间的多个空白符变成1个空格,单词与符号之间的空白符去掉;假定写入字符的空间已由调用者分配足够。
如
SELECT \r\n a1, " \"a bc \t" as a2\r FROM c\rWHERE ( a3 = 5 AND a2>1 )
应变成
SELECT a1," \"a bc \t"as a2 FROM c WHERE(a3=5 AND a2>1)
3 个解决方案
#1
关注
#2
多谢关注,能不能帮忙解决呀?
#3
public class TrimSentence {
final static char[] charsTrim = { '\n', '\b', '\r', '\f', '\'', '\"' };
final static char[] charsTrimC = { 'n', 'b', 'r', 'f', '\'', '\"' };
final static char space = ' ';
final static char slash = '\\';
final static char quotation = '"';
public static void main(String[] args) {
String str = "SELECT \r\n a1, \" \\\"a bc \\t\" as a2\r FROM c\rWHERE ( a3 = 5 AND a2>1 ) ";
System.out.println(trimSentence(str));
}
public static String trimSentence(String str) {
StringBuffer sb = new StringBuffer();
char[] charsSrc = new char[str.length()];
str.getChars(0, str.length(), charsSrc, 0);
boolean spaceFlg = false;
boolean slashFlg = false;
boolean quotationFlg = false;
for (int i = 0; i < charsSrc.length; i++) {
char ch1 = charsSrc[i];
switch (charsSrc[i]) {
case slash:
slashFlg = true;
spaceFlg = false;
if (quotationFlg)
sb.append(slash);
break;
case quotation:
if (!slashFlg) {
quotationFlg = !quotationFlg;
}
if (isSpaceDelete(sb, charsSrc[i]))
sb.deleteCharAt(sb.length() - 1);
sb.append(charsSrc[i]);
slashFlg = false;
spaceFlg = false;
break;
case space:
if (!spaceFlg) {
sb.append(charsSrc[i]);
spaceFlg = true;
}
break;
default:
if (quotationFlg) {
if (isCharsTrim(charsSrc[i])) {
sb.append(slash);
sb.append(getConveredChar(charsSrc[i]));
} else
sb.append(charsSrc[i]);
} else if (isCharsTrim(charsSrc[i])) {
charsSrc[i] = space;
i--;
continue;
} else {
if (isSpaceDelete(sb, charsSrc[i]))
sb.deleteCharAt(sb.length() - 1);
sb.append(charsSrc[i]);
}
slashFlg = false;
spaceFlg = false;
break;
}
}
return sb.toString();
}
private static boolean isCharsTrim(char ch) {
for (int i = 0; i < charsTrim.length; i++)
if (ch == charsTrim[i])
return true;
return false;
}
private static char getConveredChar(char ch) {
int i;
for (i = 0; i < charsTrim.length; i++)
if (ch == charsTrim[i])
return charsTrimC[i];
return ch;
}
private static boolean isSpaceDelete(StringBuffer sb, char ch) {
if (sb.length() <= 1)
return false;
if (sb.charAt(sb.length() - 1) != space)
return false;
return !((Character.isLetter(sb.charAt(sb.length() - 2)) || Character
.isDigit(sb.charAt(sb.length() - 2))) && (Character
.isLetter(ch) || Character.isDigit(ch)));
}
}
输出:
SELECT a1," \"a bc \t"as a2 FROM c WHERE(a3=5 AND a2>1)
final static char[] charsTrim = { '\n', '\b', '\r', '\f', '\'', '\"' };
final static char[] charsTrimC = { 'n', 'b', 'r', 'f', '\'', '\"' };
final static char space = ' ';
final static char slash = '\\';
final static char quotation = '"';
public static void main(String[] args) {
String str = "SELECT \r\n a1, \" \\\"a bc \\t\" as a2\r FROM c\rWHERE ( a3 = 5 AND a2>1 ) ";
System.out.println(trimSentence(str));
}
public static String trimSentence(String str) {
StringBuffer sb = new StringBuffer();
char[] charsSrc = new char[str.length()];
str.getChars(0, str.length(), charsSrc, 0);
boolean spaceFlg = false;
boolean slashFlg = false;
boolean quotationFlg = false;
for (int i = 0; i < charsSrc.length; i++) {
char ch1 = charsSrc[i];
switch (charsSrc[i]) {
case slash:
slashFlg = true;
spaceFlg = false;
if (quotationFlg)
sb.append(slash);
break;
case quotation:
if (!slashFlg) {
quotationFlg = !quotationFlg;
}
if (isSpaceDelete(sb, charsSrc[i]))
sb.deleteCharAt(sb.length() - 1);
sb.append(charsSrc[i]);
slashFlg = false;
spaceFlg = false;
break;
case space:
if (!spaceFlg) {
sb.append(charsSrc[i]);
spaceFlg = true;
}
break;
default:
if (quotationFlg) {
if (isCharsTrim(charsSrc[i])) {
sb.append(slash);
sb.append(getConveredChar(charsSrc[i]));
} else
sb.append(charsSrc[i]);
} else if (isCharsTrim(charsSrc[i])) {
charsSrc[i] = space;
i--;
continue;
} else {
if (isSpaceDelete(sb, charsSrc[i]))
sb.deleteCharAt(sb.length() - 1);
sb.append(charsSrc[i]);
}
slashFlg = false;
spaceFlg = false;
break;
}
}
return sb.toString();
}
private static boolean isCharsTrim(char ch) {
for (int i = 0; i < charsTrim.length; i++)
if (ch == charsTrim[i])
return true;
return false;
}
private static char getConveredChar(char ch) {
int i;
for (i = 0; i < charsTrim.length; i++)
if (ch == charsTrim[i])
return charsTrimC[i];
return ch;
}
private static boolean isSpaceDelete(StringBuffer sb, char ch) {
if (sb.length() <= 1)
return false;
if (sb.charAt(sb.length() - 1) != space)
return false;
return !((Character.isLetter(sb.charAt(sb.length() - 2)) || Character
.isDigit(sb.charAt(sb.length() - 2))) && (Character
.isLetter(ch) || Character.isDigit(ch)));
}
}
输出:
SELECT a1," \"a bc \t"as a2 FROM c WHERE(a3=5 AND a2>1)
#1
关注
#2
多谢关注,能不能帮忙解决呀?
#3
public class TrimSentence {
final static char[] charsTrim = { '\n', '\b', '\r', '\f', '\'', '\"' };
final static char[] charsTrimC = { 'n', 'b', 'r', 'f', '\'', '\"' };
final static char space = ' ';
final static char slash = '\\';
final static char quotation = '"';
public static void main(String[] args) {
String str = "SELECT \r\n a1, \" \\\"a bc \\t\" as a2\r FROM c\rWHERE ( a3 = 5 AND a2>1 ) ";
System.out.println(trimSentence(str));
}
public static String trimSentence(String str) {
StringBuffer sb = new StringBuffer();
char[] charsSrc = new char[str.length()];
str.getChars(0, str.length(), charsSrc, 0);
boolean spaceFlg = false;
boolean slashFlg = false;
boolean quotationFlg = false;
for (int i = 0; i < charsSrc.length; i++) {
char ch1 = charsSrc[i];
switch (charsSrc[i]) {
case slash:
slashFlg = true;
spaceFlg = false;
if (quotationFlg)
sb.append(slash);
break;
case quotation:
if (!slashFlg) {
quotationFlg = !quotationFlg;
}
if (isSpaceDelete(sb, charsSrc[i]))
sb.deleteCharAt(sb.length() - 1);
sb.append(charsSrc[i]);
slashFlg = false;
spaceFlg = false;
break;
case space:
if (!spaceFlg) {
sb.append(charsSrc[i]);
spaceFlg = true;
}
break;
default:
if (quotationFlg) {
if (isCharsTrim(charsSrc[i])) {
sb.append(slash);
sb.append(getConveredChar(charsSrc[i]));
} else
sb.append(charsSrc[i]);
} else if (isCharsTrim(charsSrc[i])) {
charsSrc[i] = space;
i--;
continue;
} else {
if (isSpaceDelete(sb, charsSrc[i]))
sb.deleteCharAt(sb.length() - 1);
sb.append(charsSrc[i]);
}
slashFlg = false;
spaceFlg = false;
break;
}
}
return sb.toString();
}
private static boolean isCharsTrim(char ch) {
for (int i = 0; i < charsTrim.length; i++)
if (ch == charsTrim[i])
return true;
return false;
}
private static char getConveredChar(char ch) {
int i;
for (i = 0; i < charsTrim.length; i++)
if (ch == charsTrim[i])
return charsTrimC[i];
return ch;
}
private static boolean isSpaceDelete(StringBuffer sb, char ch) {
if (sb.length() <= 1)
return false;
if (sb.charAt(sb.length() - 1) != space)
return false;
return !((Character.isLetter(sb.charAt(sb.length() - 2)) || Character
.isDigit(sb.charAt(sb.length() - 2))) && (Character
.isLetter(ch) || Character.isDigit(ch)));
}
}
输出:
SELECT a1," \"a bc \t"as a2 FROM c WHERE(a3=5 AND a2>1)
final static char[] charsTrim = { '\n', '\b', '\r', '\f', '\'', '\"' };
final static char[] charsTrimC = { 'n', 'b', 'r', 'f', '\'', '\"' };
final static char space = ' ';
final static char slash = '\\';
final static char quotation = '"';
public static void main(String[] args) {
String str = "SELECT \r\n a1, \" \\\"a bc \\t\" as a2\r FROM c\rWHERE ( a3 = 5 AND a2>1 ) ";
System.out.println(trimSentence(str));
}
public static String trimSentence(String str) {
StringBuffer sb = new StringBuffer();
char[] charsSrc = new char[str.length()];
str.getChars(0, str.length(), charsSrc, 0);
boolean spaceFlg = false;
boolean slashFlg = false;
boolean quotationFlg = false;
for (int i = 0; i < charsSrc.length; i++) {
char ch1 = charsSrc[i];
switch (charsSrc[i]) {
case slash:
slashFlg = true;
spaceFlg = false;
if (quotationFlg)
sb.append(slash);
break;
case quotation:
if (!slashFlg) {
quotationFlg = !quotationFlg;
}
if (isSpaceDelete(sb, charsSrc[i]))
sb.deleteCharAt(sb.length() - 1);
sb.append(charsSrc[i]);
slashFlg = false;
spaceFlg = false;
break;
case space:
if (!spaceFlg) {
sb.append(charsSrc[i]);
spaceFlg = true;
}
break;
default:
if (quotationFlg) {
if (isCharsTrim(charsSrc[i])) {
sb.append(slash);
sb.append(getConveredChar(charsSrc[i]));
} else
sb.append(charsSrc[i]);
} else if (isCharsTrim(charsSrc[i])) {
charsSrc[i] = space;
i--;
continue;
} else {
if (isSpaceDelete(sb, charsSrc[i]))
sb.deleteCharAt(sb.length() - 1);
sb.append(charsSrc[i]);
}
slashFlg = false;
spaceFlg = false;
break;
}
}
return sb.toString();
}
private static boolean isCharsTrim(char ch) {
for (int i = 0; i < charsTrim.length; i++)
if (ch == charsTrim[i])
return true;
return false;
}
private static char getConveredChar(char ch) {
int i;
for (i = 0; i < charsTrim.length; i++)
if (ch == charsTrim[i])
return charsTrimC[i];
return ch;
}
private static boolean isSpaceDelete(StringBuffer sb, char ch) {
if (sb.length() <= 1)
return false;
if (sb.charAt(sb.length() - 1) != space)
return false;
return !((Character.isLetter(sb.charAt(sb.length() - 2)) || Character
.isDigit(sb.charAt(sb.length() - 2))) && (Character
.isLetter(ch) || Character.isDigit(ch)));
}
}
输出:
SELECT a1," \"a bc \t"as a2 FROM c WHERE(a3=5 AND a2>1)