天天看点

JS截取与分割字符串常用技巧总结

JS截取字符串可使用 substring()或者slice()

函数:substring()

定义:substring(start,end)表示从start到end之间的字符串,包括start位置的字符但是不包括end位置的字符。

功能:字符串截取,比如想从"MinidxSearchEngine"中得到"Minidx"就要用到substring(0,6)

<code>var</code> <code>src=</code><code>"images/off_1.png"</code><code>;</code>

<code>alert(src.substring(7,10));</code>

<code>//弹出值为:off</code>

函数:substr()

定义:substr(start,length)表示从start位置开始,截取length长度的字符串。

功能:字符串截取

<code>alert(src.substr(7,3));</code>

函数:split()

功能:使用一个指定的分隔符把一个字符串分割存储到数组

<code>str=</code><code>"jpg|bmp|gif|ico|png"</code><code>;</code>

<code>arr=theString.split(</code><code>"|"</code><code>);</code>

<code>//arr是一个包含字符值"jpg"、"bmp"、"gif"、"ico"和"png"的数组</code>

函数:indexOf()

<code>var</code> <code>myString=</code><code>"JavaScript"</code><code>;</code>

<code>var</code> <code>w=myString.indexOf(</code><code>"v"</code><code>);w will be 2</code>

<code>var</code> <code>x=myString.indexOf(</code><code>"S"</code><code>);x will be 4</code>

<code>var</code> <code>y=myString.indexOf(</code><code>"Script"</code><code>);y will also be 4</code>

<code>var</code> <code>z=myString.indexOf(</code><code>"key"</code><code>);z will be -1</code>

函数:lastIndexOf()

定义:lastIndexOf()方法返回从右向左出现某个字符或字符串的首个字符索引值(与indexOf相反)

功能:返回字符串索引值

<code>alert(src.lastIndexOf(</code><code>'/'</code><code>));</code>

<code>alert(src.lastIndexOf(</code><code>'g'</code><code>));</code>

<code>//弹出值依次为:6,15</code>

补充:substr 和 substring方法的区别

substr 方法

返回一个从指定位置开始的指定长度的子字符串。

stringvar.substr(start [, length ])

参数

stringvar

必选项。要提取子字符串的字符串文字或 String 对象。

start

必选项。所需的子字符串的起始位置。字符串中的第一个字符的索引为 0。

length

可选项。在返回的子字符串中应包括的字符个数。

说明

如果 length 为 0 或负数,将返回一个空字符串。如果没有指定该参数,则子字符串将延续到 stringvar 的最后。

示例

<code>function</code> <code>SubstrDemo(){</code>

<code>  </code><code>var</code> <code>s, ss;       </code><code>// 声明变量。</code>

<code>  </code><code>var</code> <code>s =</code><code>"The rain in Spain falls mainly in the plain."</code><code>;</code>

<code>  </code><code>ss = s.substr(12, 5);</code><code>// 获取子字符串。</code>

<code>  </code><code>return</code><code>(ss);       </code><code>// 返回 "Spain"。</code>

<code>}</code>

substring 方法

返回位于 String 对象中指定位置的子字符串。

strVariable.substring(start, end)

"String Literal".substring(start, end)

指明子字符串的起始位置,该索引从 0 开始起算。

end

指明子字符串的结束位置,该索引从 0 开始起算。

substring 方法将返回一个包含从 start 到最后(不包含 end )的子字符串的字符串。

substring 方法使用 start 和 end 两者中的较小值作为子字符串的起始点。例如, strvar.substring(0, 3) 和 strvar.substring(3, 0) 将返回相同的子字符串。

如果 start 或 end 为 NaN 或者负数,那么将其替换为0。

子字符串的长度等于 start 和 end 之差的绝对值。例如,在 strvar.substring(0, 3) 和 strvar.substring(3, 0) 返回的子字符串的的长度是 3。

<code>function</code> <code>SubstringDemo(){</code>

<code>  </code><code>var</code> <code>ss;</code><code>// 声明变量。</code>

<code>  </code><code>var</code> <code>s =</code><code>"The rain in Spain falls mainly in the plain.."</code><code>;</code>

<code>  </code><code>ss = s.substring(12, 17);</code><code>// 取子字符串。</code>

<code>  </code><code>return</code><code>(ss);</code><code>// 返回子字符串。</code>

本文转自12691034博客51CTO博客,原文链接http://blog.51cto.com/12701034/1929265如需转载请自行联系原作者

笑容掩饰爱

继续阅读