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 半導體 閱讀(1477) 評論(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  回復  更多評論   

          主站蜘蛛池模板: 自治县| 平潭县| 广德县| 霍城县| 凤山县| 临汾市| 和硕县| 安远县| 东台市| 佛山市| 临安市| 柳州市| 彝良县| 普兰县| 泽库县| 庄浪县| 虹口区| 鸡西市| 图们市| 新乡县| 大丰市| 承德市| 临夏县| 铜川市| 万州区| 徐汇区| 武胜县| 京山县| 五台县| 乌兰县| 浦北县| 财经| 博罗县| 公主岭市| 富宁县| 黑河市| 汶上县| 娄底市| 通渭县| 永川市| 沂南县|