javascript去除空格函数

js去除空格自定义函数

<HTML>
<HEAD>
<TITLE>JavaScript Trim Function</TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
//****************************************************************
// Description: sInputString 为输入字符串,iType为类型,分别为
// 0 - 去除前后空格; 1 - 去左边空格; 2 - 去右边空格
//****************************************************************
function cTrim(sInputString,iType)
{
var sTmpStr = ' '
var i = -1
if(iType == 0 || iType == 1)
{
while(sTmpStr == ' ')
{
++i
sTmpStr = sInputString.substr(i,1)
}
sInputString = sInputString.substring(i)
}
if(iType == 0 || iType == 2)
{
sTmpStr = ' '
i = sInputString.length
while(sTmpStr == ' ')
{
--i
sTmpStr = sInputString.substr(i,1)
}
sInputString = sInputString.substring(0,i+1)
}
return sInputString
}
//-->
</SCRIPT>
</HEAD>
<BODY>
JavaScript去除空格函数实例:<br/><br/>
<SCRIPT LANGUAGE=javascript>
<!--
var sR0 = cTrim(" Test ",0)
var sR1 = cTrim(" Test ",1)
var sR2 = cTrim(" Test ",2)
document.write("去前后空格: '" + sR0 + "'<br/>")
document.write("去左边空格: '" + sR1 + "'<br/>")
document.write("去右边空格: '" + sR2 + "'<br/>")
//-->
</SCRIPT>
</BODY>
</HTML>

你可能感兴趣的:(JavaScript)