function gfTrim(strValue) {
var intLen = strValue.length;
var intCnt;
for (intCnt = 0; intCnt < intLen; intCnt++) {
if (strValue.indexOf(' ') == 0) {//全角空格
strValue=strValue.substring(1, intLen);
}else if (strValue.indexOf(' ') == 0) {//半角空格
strValue=strValue.substring(1, intLen);
}else if (strValue.indexOf(' ') == 0) { //TAB
strValue = strValue.substring(1, intLen);
}else {
break;
}
}
intLen = strValue.length;
for (intCnt = intLen; intCnt > 0; intCnt--) {
if (strValue.lastIndexOf(' ') == intCnt - 1 ) {//全角空格
strValue = strValue.substring(0, intCnt - 1 );
}else if (strValue.lastIndexOf(' ') == intCnt - 1) {//半角空格
strValue = strValue.substring(0, intCnt - 1 );
}else if (strValue.lastIndexOf(' ') == intCnt - 1) { //TAB
strValue = strValue.substring(0, intCnt - 1 );
}else {
break;
}
intLen = strValue.length;
}
return strValue;
}
var intLen = strValue.length;
var intCnt;
for (intCnt = 0; intCnt < intLen; intCnt++) {
if (strValue.indexOf(' ') == 0) {//全角空格
strValue=strValue.substring(1, intLen);
}else if (strValue.indexOf(' ') == 0) {//半角空格
strValue=strValue.substring(1, intLen);
}else if (strValue.indexOf(' ') == 0) { //TAB
strValue = strValue.substring(1, intLen);
}else {
break;
}
}
intLen = strValue.length;
for (intCnt = intLen; intCnt > 0; intCnt--) {
if (strValue.lastIndexOf(' ') == intCnt - 1 ) {//全角空格
strValue = strValue.substring(0, intCnt - 1 );
}else if (strValue.lastIndexOf(' ') == intCnt - 1) {//半角空格
strValue = strValue.substring(0, intCnt - 1 );
}else if (strValue.lastIndexOf(' ') == intCnt - 1) { //TAB
strValue = strValue.substring(0, intCnt - 1 );
}else {
break;
}
intLen = strValue.length;
}
return strValue;
}
博客给出了一段JavaScript代码,定义了名为gfTrim的函数,用于去除字符串首尾的全角空格、半角空格和TAB字符。函数通过循环和字符串截取操作,分别处理字符串开头和结尾的空白字符,最终返回处理后的字符串。
273

被折叠的 条评论
为什么被折叠?



