indexOf 方法
返回 String 对象内第一次出现子字符串的字符位置。 strObj.indexOf(subString[, startIndex])参数
strObj必选项。String 对象或文字。subString必选项。要在 String 对象中查找的子字符串。starIndex可选项。该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。说明
indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。 如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。 从左向右执行查找。否则,该方法与 lastIndexOf 相同。示例
下面的示例说明了 indexOf 方法的用法。function IndexDemo(str2){
   var str1 = "BABEBIBOBUBABEBIBOBU"
   var s = str1.indexOf(str2);
   return(s);
}

解决方案 »

  1.   

    indexOf Method
    Returns the character position where the first occurrence of a substring occurs within a String object. strObj.indexOf(subString[, startIndex])Arguments
    strObjRequired. A String object or literal.subStringRequired. Substring to search for within the String object.startIndexOptional. Integer value specifying the index to begin searching within the String object. If omitted, searching starts at the beginning of the string.Res
    The indexOf method returns an integer value indicating the beginning of the substring within the String object. If the substring is not found, a -1 is returned. If startindex is negative, startindex is treated as zero. If it is larger than the greatest character position index, it is treated as the largest possible index. Searching is performed from left to right. Otherwise, this method is identical to lastIndexOf.Example
    The following example illustrates the use of the indexOf method.function IndexDemo(str2){
       var str1 = "BABEBIBOBUBABEBIBOBU"
       var s = str1.indexOf(str2);
       return(s);
    }
      

  2.   

    <% 
       if request.form("result")=100 then 
         str1="1982ewqu"
         str2="e"
         xxx=Instr(str1,str2)
         response.write xxx
       end if
    %>
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <title>New Page 1</title>
    <script language="javascript">
    function testdata()
    {
      var str1="1982ewqu";
      var str2="e";
      alert(str1.indexOf(str2));
    }
    </script></head>
    <body onload="testdata();">
    <form method="POST" action="" name="DFrm" id="DFrm">
      <input type="text" name="bh" id="bh" size="13" class="input">
      <input type="hidden" name="result" value=100> 
      <input type="button" value="查看" onclick="DFrm.submit();"> 
    </form> 
    <p>javascript:indexOf()返回 String 对象内第一次出现子字符串的字符位置。它与vbscript中的Instr()的作用相当</p> <p>vbscript:返回搜寻字符串的第一个位置</p></body></html>
      

  3.   

    <script language=javascript>var tt="23523546346";
    if (tt.indexOf(".")!=-1)
    {
    alert("yes!")
    alert(tt.indexOf("."));
    }
      

  4.   

    indexOf()的用法是判断一个字符串中是否有给定的特殊字符具体用法是: 字符串 str   需要判断的字符