« WordPress最简洁google的sitemap VitaEditor比FCKeditor和TinyMce精简的编辑器 »

javascript中split函数使用说明

使用javascript中的split函数分割字符串示例:

<script>   
  alert("c:\www\ccvita.jpg");     
  var   path="c:\\www\\ccvita.jpg";   
  var   arr=path.split("\\");   
  alert(arr);   
  </script>

下面是函数的具体介绍:

split 方法

将一个字符串分割为子字符串,然后将结果作为字符串数组返回。

stringObj.split([separator[, limit]])

参数

stringObj

必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。

separator

可选项。字符串或 正则表达式 对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽略该选项,返回包含整个字符串的单一元素数组。

limit

可选项。该值用来限制返回数组中的元素个数。

说明

split 方法的结果是一个字符串数组,在 stingObj 中每个出现 separator 的位置都要进行分解。separator 不作为任何数组元素的部分返回。

示例

下面的示例演示了 split 方法的用法。

function SplitDemo(){
   var s, ss;
   var s = "The rain in Spain falls mainly in the plain.";
   // 在每个空格字符处进行分解。
   ss = s.split(" ");
   return(ss);
}

“javascript中split函数使用说明”

2条回复
  1. 不错,不错

  2. 很多情况下,用正则来分割字符串到数组是一个不错的主意:

    var s=’2008-04-01 13:05:08′;
    var arr=s.split(/\D+/);
    alert(arr);

    这样, 年,月,日,时,分,秒
    就保存在数组 arr 里面了

回复留言

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

你可以使用XHTML标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>