JavaScript使用indexOf搜索字符串并返回位置,由于效果比较简单,就不再使用运行功能给大家演示了,需要的朋友自己将代码复制到任意HTML中即可测试。
Copy to Clipboard 引用的内容:[www.veryhuo.com]
引用的内容:[www.veryhuo.com]
<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>