javascript string对象方法总结

hszr7731 7年前
   <p>1.anchor()</p>    <p>用于创建html锚,也就是a标签,()中可以带参数,是a标签的name属性值。</p>    <pre>  <code class="language-javascript">    var string="hello world";      document.writeln(string.anchor("name属性值"));  </code></pre>    <p>2.charAt()</p>    <p>用于寻找特定索引值的字符,()中的参数是字符串中某一字符的索引值,注意charAt()只能返回一个字符。</p>    <pre>  <code class="language-javascript">    var string="hello world";      document.writeln(string.charAt(4));//o  </code></pre>    <p>3concat()</p>    <p>用于拼接字符串,()中的参数是一个或多个字符串,注意:一般字符串拼接我们用+就可以实现。</p>    <pre>  <code class="language-javascript">    var string1="hello ";      var string2="world";      document.writeln(string1.concat(string2));  </code></pre>    <p>4.indexOf()</p>    <p>用于寻找某一字符或字符串第一次出现的索引值,()中的第一个参数是要寻找的字符串或字符,第二个参数是要从哪个索引位置寻找,如果省略则从头向尾部寻找,没有找到就会返回-1.注意:indexOf()对大小写敏感</p>    <pre>  <code class="language-javascript">    var string1="hello world";      var string2="world";      document.writeln(string1.indexOf(string2,3));//6  </code></pre>    <pre>  <code class="language-javascript">    var string1="hello world";      var string2="word";      document.writeln(string1.indexOf(string2,3));//-1  </code></pre>    <p>5.lastIndexOf()</p>    <p>用于寻找某一字符或字符串最后一个出现的索引值,()中的第一个参数是要寻找的字符串或字符,第二个参数是要从哪个索引位置寻找,如果省略则从尾部向头部寻找,没有找到就会返回-1.注意:indexOf()对大小写敏感</p>    <pre>  <code class="language-javascript">    var string1="hello world";      var string2="world";      document.writeln(string1.lastIndexOf(string2,5));//-1  </code></pre>    <p>6.match()</p>    <p>用于寻找匹配的某一字符或字符串,通常和正则表达式配合使用,如果正则表达式是全局搜索,则返回所有匹配的字符或字符串的数组。</p>    <pre>  <code class="language-javascript">    var string="123123123";      document.writeln(string.match(/2/g));//2,2,2  </code></pre>    <p>7.replace()</p>    <p>用于替换某一字符或字符串,()中的第一个参数是替换前的字符串,可以是字符串或正则表达式,第二个参数是替换后的字符串,只能是字符串。</p>    <pre>  <code class="language-javascript">    var string="hello world";      document.writeln(string.replace("hello","hi"));  </code></pre>    <p>8.search()</p>    <p>用于查找第一个匹配正则表达式的字符串的索引值,注意:1.search()没有全局搜索能力,就算写了g。2.search()只能搜索第一个匹配的字符串位置。3.对大小写敏感。</p>    <pre>  <code class="language-javascript">    var string="hello world";      document.writeln(string.search("hello"));  </code></pre>    <p>9.slice()</p>    <p>用于截取匹配的字符串,()中的参数都是索引值,而且可以为负数。返回的字符串包括第一个参数的字符,不包括第二个参数的字符。</p>    <pre>  <code class="language-javascript">    var string="hello world";      document.writeln(string.slice(3,5));//lo  </code></pre>    <p>10.split()</p>    <p>将字符串转化成数组。()中的第一个参数为按什么截取,第二个参数是截取多少个。</p>    <pre>  <code class="language-javascript">    var string="hello world";      document.writeln(string.split(" "));//hello,world  </code></pre>    <p>11.substr()</p>    <p>用于截取匹配的字符串,()中一个参数是开始匹配的索引值,第二个参数是要截取的长度。</p>    <pre>  <code class="language-javascript">    var string="hello world";      document.writeln(string.substr(2,6));//llo wo  </code></pre>    <p>12.substring()</p>    <p>和slice()用法一样,只不过substring()的参数不接受负值,而且如果start>end,他会将这俩个参数自动换位。</p>    <p>13.valueOf()</p>    <p>用于检验调用该方法的对象是不是string对象。</p>    <p> </p>    <p>来自:http://www.cnblogs.com/iwebkit/p/6504583.html</p>    <p> </p>