昨天,遇到了一个技术问题。本来我在程序中用的trim()方法来处理从JSP页面传来的值,后来在测试时,发现当我输入的是全角空格时,trim()方法失效。
需求是这样的,只是去掉字符串两端的空格(不论是全角空格还是半角空格),字符串中间的空格不变。后来经过查资料和研究,总结出了以下两种写法:
一、字符串的字符替换法:
思路:(1)将字符串中所以有的全角空格转换成两个半角空格
(2)用trim()方法去掉字符串两端的空格(此时全是半角空格)
(3)将两个相邻的半角合成一个全角
评价:这方法遇到字符串中间是半角+全角的时间,(3)步中有可能合并后,与原来
不一样。
原代码:
public String trim(String Str){
String newstr = Str.replaceAll(" "," ");
newstr = newstr.trim();
newstr = newstr.replaceAll(" "," ");
return newstr;
}
String newstr = Str.replaceAll(" "," ");
newstr = newstr.trim();
newstr = newstr.replaceAll(" "," ");
return newstr;
}
二、正则表达式转换法:
思路:利用正则表达式进行匹配替换。
评价:这应该是正统的解决之道
原代码:
例1:
public static String trims(String Str,String Flag) {
if (Str == null || Str.equals("")) {
return Str;
} else {
Str = ""+Str;
if( Flag == "l" || Flag == "L" )/*trim left side only*/
{
String RegularExp = "^[ ]+";
return Str.replaceAll(RegularExp,"");
}
else if( Flag == "r" || Flag == "R" )/*trim right side only*/
{
String RegularExp = "[ ]+$";
return Str.replaceAll(RegularExp,"");
}
else/*defautly, trim both left and right side*/
{
String RegularExp = "^[ ]+|[ ]+$";
return Str.replaceAll(RegularExp,"");
}
if (Str == null || Str.equals("")) {
return Str;
} else {
Str = ""+Str;
if( Flag == "l" || Flag == "L" )/*trim left side only*/
{
String RegularExp = "^[ ]+";
return Str.replaceAll(RegularExp,"");
}
else if( Flag == "r" || Flag == "R" )/*trim right side only*/
{
String RegularExp = "[ ]+$";
return Str.replaceAll(RegularExp,"");
}
else/*defautly, trim both left and right side*/
{
String RegularExp = "^[ ]+|[ ]+$";
return Str.replaceAll(RegularExp,"");
}
}
}
}
例2:
public static String trim(String str) {
if (str == null || str.equals("")) {
return str;
} else {
//return leftTrim(rightTrim(str));
return str.replaceAll("^[ ]+|[ ]+$","");
}
}
if (str == null || str.equals("")) {
return str;
} else {
//return leftTrim(rightTrim(str));
return str.replaceAll("^[ ]+|[ ]+$","");
}
}
public static String leftTrim(String str) {
if (str == null || str.equals("")) {
return str;
} else {
return str.replaceAll("^[ ]+", "");
}
}
if (str == null || str.equals("")) {
return str;
} else {
return str.replaceAll("^[ ]+", "");
}
}
public static String rightTrim(String str) {
if (str == null || str.equals("")) {
return str;
} else {
return str.replaceAll("[ ]+$", "");
}
}
if (str == null || str.equals("")) {
return str;
} else {
return str.replaceAll("[ ]+$", "");
}
}
给大家一个综合的测试类:
public class Test {
public static void main(String[] args) {
System.out.println("leftTrim:" + leftTrim(" 123 123 123 ") + ":");
System.out.println("rightTrim:" + rightTrim(" 123 123 123 ") + ":");
System.out.println("trim:" + trim(" 123 123 123 ") + ":");
System.out.println("leftTrim:" + trims(" 123 123 123 ","l") + ":");
System.out.println("rightTrim:" + trims(" 123 123 123 ","r") + ":");
System.out.println("trim:" + trims(" 123 123 123 ","a") + ":");
}
System.out.println("leftTrim:" + leftTrim(" 123 123 123 ") + ":");
System.out.println("rightTrim:" + rightTrim(" 123 123 123 ") + ":");
System.out.println("trim:" + trim(" 123 123 123 ") + ":");
System.out.println("leftTrim:" + trims(" 123 123 123 ","l") + ":");
System.out.println("rightTrim:" + trims(" 123 123 123 ","r") + ":");
System.out.println("trim:" + trims(" 123 123 123 ","a") + ":");
}
public static String trim(String str) {
if (str == null || str.equals("")) {
return str;
} else {
//return leftTrim(rightTrim(str));
return str.replaceAll("^[ ]+|[ ]+$","");
}
}
if (str == null || str.equals("")) {
return str;
} else {
//return leftTrim(rightTrim(str));
return str.replaceAll("^[ ]+|[ ]+$","");
}
}
public static String leftTrim(String str) {
if (str == null || str.equals("")) {
return str;
} else {
return str.replaceAll("^[ ]+", "");
}
}
if (str == null || str.equals("")) {
return str;
} else {
return str.replaceAll("^[ ]+", "");
}
}
public static String rightTrim(String str) {
if (str == null || str.equals("")) {
return str;
} else {
return str.replaceAll("[ ]+$", "");
}
}
if (str == null || str.equals("")) {
return str;
} else {
return str.replaceAll("[ ]+$", "");
}
}
public static String trims(String Str,String Flag) {
if (Str == null || Str.equals("")) {
return Str;
} else {
Str = ""+Str;
if( Flag == "l" || Flag == "L" )/*trim left side only*/
{
String RegularExp = "^[ ]+";
return Str.replaceAll(RegularExp,"");
}
else if( Flag == "r" || Flag == "R" )/*trim right side only*/
{
String RegularExp = "[ ]+$";
return Str.replaceAll(RegularExp,"");
}
else/*defautly, trim both left and right side*/
{
String RegularExp = "^[ ]+|[ ]+$";
return Str.replaceAll(RegularExp,"");
}
}
}
}
if (Str == null || Str.equals("")) {
return Str;
} else {
Str = ""+Str;
if( Flag == "l" || Flag == "L" )/*trim left side only*/
{
String RegularExp = "^[ ]+";
return Str.replaceAll(RegularExp,"");
}
else if( Flag == "r" || Flag == "R" )/*trim right side only*/
{
String RegularExp = "[ ]+$";
return Str.replaceAll(RegularExp,"");
}
else/*defautly, trim both left and right side*/
{
String RegularExp = "^[ ]+|[ ]+$";
return Str.replaceAll(RegularExp,"");
}
}
}
}
如果你也遇到这样的问题,希望以上的文字能帮助到你:),欢迎光临我的博客!