Prototype - truncate() 方法

此方法将字符串截断为给定的长度,并在其后附加后缀(表示它只是摘录)。

如果未指定,则长度参数默认为 30,后缀默认为"..."。

语法

string.truncate([length = 30[, suffix = '...']]) ;

返回值

返回截断的字符串。

示例

<html>
   <head>
      <title>Prototype examples</title>
      <script type = "text/javascript" src = "/javascript/prototype.js"></script>
      
      <script>
         function showResult() {
            var str = 'A random sentence whose length exceeds 30 characters.';
            alert("str.truncate() : " + str.truncate() );
            alert("str.truncate( 10 ) : " + str.truncate(10) );
            alert("str.truncate( 10, '...') : " + 
            str.truncate(10, '...') );
         }
      </script>
   </head>

   <body>
      <p>单击按钮查看结果。</p>
      <br />
      <br />
      <input type = "button" value = "Result" onclick = "showResult();"/>
   </body>
</html>

输出

prototype_string_processing.html