下面的代码是js代码,可以参考以下。function trimSpace( str ){ var len = str.length; var RetStr = ""; var Lpos = 0; var Rpos = len - 1; var flag = false; var i = 0; for ( i = 0; i < len; i++ ){ if ( str.charAt( i ) != " " && str.charAt( i ) != " " ){ Lpos = i; flag = true; break; } } for ( i = Rpos; i > -1; i-- ){ if ( str.charAt( i ) != " " && str.charAt( i ) != " " ){ Rpos = i; flag = true; break; } } if(flag){ for ( i = Lpos; i <= Rpos; i++ ){ RetStr += str.charAt( i ); } return RetStr; }else{ return ""; } }
同时,需要注意的是:trim() This method may be used to trim whitespace from the beginning and end of a string; in fact, it trims all ASCII control characters as well. 它除了截断空格外,还截断两端所有的控制字符。共5种字符(而我们上面给出的方法可以截断任何多个任意的字符): '\t' '\u0009' HORIZONTAL TABULATION '\n' '\u000A' NEW LINE '\f' '\u000C' FORM FEED '\r' '\u000D' CARRIAGE RETURN ' ' '\u0020' SPACE
半角1个字节:byte a = '1';
全角2个字节:dst[0]=0xA3;
dst[1]=(byte)(0x80+a) System.out.println(new String(dst));
Pattern p = Pattern.compile("[ ]*");//匹配一个或多个空格
Matcher m = p.matcher(strIn);//strIn要检查的字符串
boolean b = m.matches();
if (b) {strIn= m.replaceAll("");} //删除匹配的字符串不过前面要import java.util.regex.Pattern;
import java.util.regex.Matcher;
你的正则表达式会把中间的空格都给去了的不知道怎么解决,觉得应该从它们的ASSIC码之间的转换关系去考虑,类似于1楼的。
var len = str.length;
var RetStr = "";
var Lpos = 0;
var Rpos = len - 1;
var flag = false;
var i = 0;
for ( i = 0; i < len; i++ ){
if ( str.charAt( i ) != " " && str.charAt( i ) != " " ){
Lpos = i;
flag = true;
break;
}
}
for ( i = Rpos; i > -1; i-- ){
if ( str.charAt( i ) != " " && str.charAt( i ) != " " ){
Rpos = i;
flag = true;
break;
}
}
if(flag){
for ( i = Lpos; i <= Rpos; i++ ){
RetStr += str.charAt( i );
}
return RetStr;
}else{
return "";
}
}
1。使用trim();
2。循环去除两边的全角空格。 String s = " Hello World "; // 前部含有一个半角一个全角的空格
s = s.trim();
while (s.charAt(0) == ' ') { // 删除前部所有的全角空格
s = s.substring(1);
}
while (s.charAt(s.length() - 1) == ' ') { // 删除后部所有的全角空格
s = s.substring(0, s.length() - 1);
}
System.out.println(s);
最好的办法是写一个自定义的函数 myTrim(String source, String toTrim);返回把source两端的所有的toTrim截掉的字符串。 String myTrim(String source, String toTrim){
StringBuffer sb = new StringBuffer(source);
while(toTrim.indexOf(new Character(sb.charAt(0)).toString()) != -1) {
sb.deleteCharAt(0);
}
while(toTrim.indexOf(new Character(sb.charAt(sb.length() - 1)).toString()) != -1) {
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}其中最二个参数是需要去掉的单个字符组成的字符串,如想去掉全角和半角的空格,则第二个参数为" "。
针对上面的例子,可以写成:s = myTrim(s, " ");就行了。当然,这个方法是通用的,去掉两端的任一字符均可。
This method may be used to trim whitespace from the beginning and end of a string; in fact, it trims all ASCII control characters as well. 它除了截断空格外,还截断两端所有的控制字符。共5种字符(而我们上面给出的方法可以截断任何多个任意的字符):
'\t' '\u0009' HORIZONTAL TABULATION
'\n' '\u000A' NEW LINE
'\f' '\u000C' FORM FEED
'\r' '\u000D' CARRIAGE RETURN
' ' '\u0020' SPACE
{
p_str=p_str.replaceAll("A","A");
p_str=p_str.replaceAll("B","B");
p_str=p_str.replaceAll("C","C");
p_str=p_str.replaceAll("D","D");
p_str=p_str.replaceAll("E","E");
p_str=p_str.replaceAll("F","F");
p_str=p_str.replaceAll("G","G");
p_str=p_str.replaceAll("H","H");
p_str=p_str.replaceAll("I","I");
p_str=p_str.replaceAll("J","J");
p_str=p_str.replaceAll("K","K");
p_str=p_str.replaceAll("L","L");
p_str=p_str.replaceAll("M","M");
p_str=p_str.replaceAll("N","N");
p_str=p_str.replaceAll("O","O");
p_str=p_str.replaceAll("P","P");
p_str=p_str.replaceAll("Q","Q");
p_str=p_str.replaceAll("R","R");
p_str=p_str.replaceAll("S","S");
p_str=p_str.replaceAll("T","T");
p_str=p_str.replaceAll("U","U");
p_str=p_str.replaceAll("V","V");
p_str=p_str.replaceAll("W","W");
p_str=p_str.replaceAll("X","X");
p_str=p_str.replaceAll("Y","Y");
p_str=p_str.replaceAll("Z","Z"); p_str=p_str.replaceAll("a","A");
p_str=p_str.replaceAll("b","B");
p_str=p_str.replaceAll("c","C");
p_str=p_str.replaceAll("d","D");
p_str=p_str.replaceAll("e","E");
p_str=p_str.replaceAll("f","F");
p_str=p_str.replaceAll("g","G");
p_str=p_str.replaceAll("h","H");
p_str=p_str.replaceAll("i","I");
p_str=p_str.replaceAll("j","J");
p_str=p_str.replaceAll("k","K");
p_str=p_str.replaceAll("l","L");
p_str=p_str.replaceAll("m","M");
p_str=p_str.replaceAll("n","N");
p_str=p_str.replaceAll("o","O");
p_str=p_str.replaceAll("p","P");
p_str=p_str.replaceAll("q","Q");
p_str=p_str.replaceAll("r","R");
p_str=p_str.replaceAll("s","S");
p_str=p_str.replaceAll("t","T");
p_str=p_str.replaceAll("u","U");
p_str=p_str.replaceAll("v","V");
p_str=p_str.replaceAll("w","W");
p_str=p_str.replaceAll("x","X");
p_str=p_str.replaceAll("y","Y");
p_str=p_str.replaceAll("z","Z"); p_str=p_str.replaceAll("a","A");
p_str=p_str.replaceAll("b","B");
p_str=p_str.replaceAll("c","C");
p_str=p_str.replaceAll("d","D");
p_str=p_str.replaceAll("e","E");
p_str=p_str.replaceAll("f","F");
p_str=p_str.replaceAll("g","G");
p_str=p_str.replaceAll("h","H");
p_str=p_str.replaceAll("i","I");
p_str=p_str.replaceAll("j","J");
p_str=p_str.replaceAll("k","K");
p_str=p_str.replaceAll("l","L");
p_str=p_str.replaceAll("m","M");
p_str=p_str.replaceAll("n","N");
p_str=p_str.replaceAll("o","O");
p_str=p_str.replaceAll("p","P");
p_str=p_str.replaceAll("q","Q");
p_str=p_str.replaceAll("r","R");
p_str=p_str.replaceAll("s","S");
p_str=p_str.replaceAll("t","T");
p_str=p_str.replaceAll("u","U");
p_str=p_str.replaceAll("v","V");
p_str=p_str.replaceAll("w","W");
p_str=p_str.replaceAll("x","X");
p_str=p_str.replaceAll("y","Y");
p_str=p_str.replaceAll("z","Z");
p_str=p_str.replaceAll("0","0");
p_str=p_str.replaceAll("1","1");
p_str=p_str.replaceAll("2","2");
p_str=p_str.replaceAll("3","3");
p_str=p_str.replaceAll("4","4");
p_str=p_str.replaceAll("5","5");
p_str=p_str.replaceAll("6","6");
p_str=p_str.replaceAll("7","7");
p_str=p_str.replaceAll("8","8");
p_str=p_str.replaceAll("9","9"); p_str = p_str.replaceAll("\\.", ""); //烂java, 还要转意。
p_str = p_str.replaceAll("\\。", "");
p_str = p_str.replaceAll("\\.", "");
p_str = p_str.replaceAll("\\*", "");
p_str = p_str.replaceAll("\\\u30FB", "");
p_str = p_str.replaceAll("\\-", "");
p_str = p_str.replaceAll("\\_", "");
p_str = p_str.replaceAll("\\-", "");
p_str=p_str.replaceAll("\\/","");
p_str=p_str.trim();
p_str=p_str.replaceAll(" +", "");//去中间空格
p_str=p_str.replaceAll(" +", "");//去全角中间空格
// p_str=p_str.replaceAll("\\","");//烂java, 这个转不出来了。
return p_str;
}