js使用indexOf搜索字符串并返回位置
來源:程序員人生 發布時間:2014-06-17 16:35:27 閱讀次數:2676次
JavaScript使用indexOf搜索字符串并返回位置,由于效果比較簡單,就不再使用運行功能給大家演示了,需要的朋友自己將代碼復制到任意HTML中即可測試。
<html>
<head>
<title>搜索字符串 - Liehuo.Net</title>
</head>
<body>
<script language="javascript">
<!--
str = "JavaScript";
ch = "S";
index = str.indexOf(ch);
document.write("str=" + str + "<br>");
if (index != -1){
document.write( ch + "的位置是:" + index);
}else{
document.write("沒有找到你要搜索的字符串!");
}
//-->
</script>
</body>
</html>