javascript,jQuery,trim()

JavaScript trim()

Syntax

string.trim()
  • The trim() method removes whitespace from both sides of a string.
  • The trim() method does not change the original string.

Example1

 var str = "       Hello World!        ";
alert(str.trim());

Example2

 function myTrim(x) {
return x.replace(/^\s+|\s+$/gm,'');
} function myFunction() {
var str = myTrim(" Hello World! ");
alert(str);
}

jQuery.trim()

  • Remove the whitespace from the beginning and end of a string.

Syntax

jQuery.trim( str )

Example

 <script>
var str = " lots of spaces before and after ";
$( "#original" ).html( "Original String: '" + str + "'" );
$( "#trimmed" ).html( "$.trim()'ed: '" + $.trim(str) + "'" );
</script>
上一篇:Svg path画线(不管是直线还是曲线)在一定情况下线条的宽度不一的情况(记录)


下一篇:POJ2151-Check the difficulty of problems(概率DP)