JBOSS 点滴

丰丰的博客

多选 截取 lastIndexOf() 方法 http://www.w3school.com.cn/js/jsref_lastIndexOf.asp

如果在做多选时,出现"objCode=001,002,003,"
要去掉最后一个逗号,处理办法:
objCode=objCode.substring(0,objCode.lastIndexOf(","));



JavaScript lastIndexOf() 方法

定义和用法

lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。

语法

stringObject.lastIndexOf(searchvalue,fromindex)
参数 描述
searchvalue 必需。规定需检索的字符串值。
fromindex 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。

返回值

如果在 stringObject 中的 fromindex 位置之前存在 searchvalue,则返回的是出现的最后一个 searchvalue 的位置。

说明

该方法将从尾到头地检索字符串 stringObject,看它是否含有子串 searchvalue。开始检索的位置在字符串的 fromindex 处或字符串的结尾(没有指定 fromindex 时)。如果找到一个 searchvalue,则返回 searchvalue 的第一个字符在 stringObject 中的位置。stringObject 中的字符位置是从 0 开始的。

提示和注释

注释:lastIndexOf() 方法对大小写敏感!

注释:如果要检索的字符串值没有出现,则该方法返回 -1。

实例

在本例中,我们将在 "Hello world!" 字符串内进行不同的检索:

<script type="text/javascript">
var str="Hello world!"
document.write(str.lastIndexOf("Hello") + "<br />")
document.write(str.lastIndexOf("World") + "<br />")
document.write(str.lastIndexOf("world"))
</script>

以上代码的输出:

0
-1
6

TIY

lastIndexOf()
如何使用 lastIndexOf() 在字符串内进行检索。

posted on 2009-11-12 11:47 半导体 阅读(1455) 评论(3)  编辑  收藏 所属分类: javascript

评论

# re: 多选 截取 lastIndexOf() 方法 http://www.w3school.com.cn/js/jsref_lastIndexOf.asp 2010-04-15 17:59 小成

太谢谢了,我刚好遇到多选的那个问题,没想到在这找到了答案,哈哈~  回复  更多评论   

# re: 多选 截取 lastIndexOf() 方法 http://www.w3school.com.cn/js/jsref_lastIndexOf.asp 2010-07-08 22:56 半导体

@小成

很高兴能帮助到你!  回复  更多评论   

# re: 多选 截取 lastIndexOf() 方法 http://www.w3school.com.cn/js/jsref_lastIndexOf.asp 2012-10-04 14:44 W3School

http://www.w3school.org.cn/js/jsref_lastIndexOf.asp  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: