---
title: "indexOf() 检索字符串"
author: "小丑鱼0703"
date: "2015-01-27T17:24:51Z"
tags: []
categories: ["Web前端"]
slug: "indexof"
draft: false
lastmod: "2015-01-27T17:24:51Z"
id: 2180533
url: "https://wangjingyi.iteye.com/blog/2180533"
---

indexOf() 返回某个指定的字符串值在字符串中<span style="color:red">首次</span>出现的位置。

1.头到尾地检索字符串，看是否含有子串
2.字符位置是从 0 开始的。
3.indexOf() 方法对<span style="color:red">大小写敏感</span>！
4.要检索的字符串值<span style="color:blue">没有</span>出现，则该方法<span style="color:blue">返回 -1</span>


```javascript

var str="Hello,WJY!I'm WJY not Wjy!";
alert(str.indexOf("Hello"));//"0"
alert(str.indexOf("WJY"));//"6"
alert(str.indexOf("wjy"));//"-1"
```