好像javascript中没有trim函数,应该用什么代替呢?

xgy_p 2004-05-08 11:11:34
我要去掉某个字符串头尾的空格,应该怎样做呢?难道要一个个字符地charAt,然后再判断它是不是空格?另外一个延伸出来的问题是,我要知道某个字符串是否全部由空格造成的,我应该怎样做?
...全文
218 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingchang2000 2004-09-16
  • 打赏
  • 举报
回复
呵呵,都是正则表达式,你在添上一个全角空格吧,反正都要被trim掉的
cuixiping 2004-09-16
  • 打赏
  • 举报
回复
String.prototype.Trim = function(){return this.replace(/^\s+|\s+$/g,"");}

alert(" ".Trim()=="")
心云意水 2004-05-08
  • 打赏
  • 举报
回复
当一个字符串trim后,长度为0表示该字符串全部由空格组成
<script language="JavaScript">
String.prototype.trim = function () {
return this.replace(/^\s*|\s*$/g,'');
};
alert(" hehe ".trim());
if(" ".trim().length==0)
alert("全部是空格!")
</script>
joe820302 2004-05-08
  • 打赏
  • 举报
回复
<SCRIPT LANGUAGE="Javascript">
/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
PURPOSE: Remove leading blanks from our string.
IN: str - the string we want to LTrim
*/
{
var whitespace = new String(" \t\n\r");

var s = new String(str);

if (whitespace.indexOf(s.charAt(0)) != -1) {
// We have a string with leading blank(s)...

var j=0, i = s.length;

// Iterate from the far left of string until we
// don't have any more whitespace...
while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
j++;

// Get the substring from the first non-whitespace
// character to the end of the string...
s = s.substring(j, i);
}
return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
PURPOSE: Remove trailing blanks from our string.
IN: str - the string we want to RTrim

*/
{
// We don't want to trip JUST spaces, but also tabs,
// line feeds, etc. Add anything else you want to
// "trim" here in Whitespace
var whitespace = new String(" \t\n\r");

var s = new String(str);

if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
// We have a string with trailing blank(s)...

var i = s.length - 1; // Get length of string

// Iterate from the far right of string until we
// don't have any more whitespace...
while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
i--;


// Get the substring from the front of the string to
// where the last non-whitespace character is...
s = s.substring(0, i+1);
}

return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
PURPOSE: Remove trailing and leading blanks from our string.
IN: str - the string we want to Trim

RETVAL: A Trimmed string!
*/
{
return RTrim(LTrim(str));
}

alert(Trim(" abc "));
alert(Trim(" ")=="");
</SCRIPT>
ghlfllz 2004-05-08
  • 打赏
  • 举报
回复
/*************************************************************
名称:trim
功能:去掉左右两边的空格
参数:sourcestr,源字符串;
返回:空格去完的字符串
*************************************************************/
function trim(sourcestr) {
sourcestr=sourcestr.replace(/^\s*/,"");
return sourcestr.replace(/\s*$/,"");
}
fason 2004-05-08
  • 打赏
  • 举报
回复
<script language="JavaScript">
String.prototype.trim = function () {
return this.replace(/^\s*|\s*$/g,'');
};
alert(" hehe ".trim());
</script>

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧