Phương thức
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
7, đưa ra một đối số. một chuỗi con để tìm kiếm, tìm kiếm toàn bộ chuỗi gọi và trả về chỉ mục của lần xuất hiện đầu tiên của chuỗi con đã chỉ định. Đưa ra một đối số thứ hai. một số, phương thức trả về lần xuất hiện đầu tiên của chuỗi con đã chỉ định tại một chỉ số lớn hơn hoặc bằng số đã chỉ địnhThử nó
cú pháp
indexOf[searchString]
indexOf[searchString, position]
Thông số
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
0Chuỗi con để tìm kiếm, bị ép buộc vào một chuỗi
Nếu phương thức được gọi mà không có đối số, thì
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
0 bị ép thành "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
2. Do đó,"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
3 trả về "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
4 — bởi vì chuỗi con "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
2 được tìm thấy ở vị trí "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
4 trong chuỗi "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
2. Nhưng "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
8, trả về "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
9 — vì chuỗi con "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
2 không được tìm thấy trong chuỗi "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
21"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
22 Tùy chọnPhương thức trả về chỉ mục của lần xuất hiện đầu tiên của chuỗi con đã chỉ định tại vị trí lớn hơn hoặc bằng
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
22, mặc định là "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
4. Nếu "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
22 lớn hơn độ dài của chuỗi gọi, thì phương thức này hoàn toàn không tìm kiếm chuỗi gọi. Nếu "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
22 nhỏ hơn 0, phương thức sẽ hoạt động như nếu "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
22 là "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
4
29 trả về"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
10 — bởi vì nó khiến phương thức hoạt động như thể đối số thứ hai là"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
4 và lần xuất hiện đầu tiên của"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
12 tại vị trí lớn hơn hoặc bằng"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
4 là tại vị trí"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
10"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
15 trả về"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
9 — bởi vì, mặc dù đúng là chuỗi con"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
17 xuất hiện tại chỉ mục"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
18, nhưng vị trí đó không lớn hơn hoặc bằng"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
19"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
10 trả về"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
9 - vì"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
12 lớn hơn độ dài của"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
13, khiến phương thức không tìm kiếm chuỗi nào cả"hello world".indexOf[""]; // returns 0 "hello world".indexOf["", 0]; // returns 0 "hello world".indexOf["", 3]; // returns 3 "hello world".indexOf["", 8]; // returns 8
Giá trị trả về
Chỉ số về lần xuất hiện đầu tiên của
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
0 được tìm thấy hoặc "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
9 nếu không tìm thấyTrả về giá trị khi sử dụng chuỗi tìm kiếm trống
Tìm kiếm một chuỗi tìm kiếm trống tạo ra kết quả lạ. Không có đối số thứ hai hoặc có đối số thứ hai có giá trị nhỏ hơn độ dài của chuỗi gọi, giá trị trả về giống với giá trị của đối số thứ hai
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
Tuy nhiên, với đối số thứ hai có giá trị lớn hơn hoặc bằng độ dài của chuỗi, giá trị trả về là độ dài của chuỗi
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
2Trong ví dụ trước, phương thức hoạt động như thể nó tìm thấy một chuỗi trống ngay sau vị trí được chỉ định trong đối số thứ hai. Trong trường hợp thứ hai, phương thức hoạt động như thể nó tìm thấy một chuỗi trống ở cuối chuỗi gọi
Sự mô tả
Các chuỗi không được lập chỉ mục. Chỉ mục của ký tự đầu tiên của chuỗi là
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
4 và chỉ mục của ký tự cuối cùng của chuỗi là độ dài của chuỗi trừ đi 1"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
1Phương pháp
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
7 phân biệt chữ hoa chữ thường. Ví dụ, biểu thức sau trả về "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
9"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
1Kiểm tra sự xuất hiện
Khi kiểm tra xem một chuỗi con cụ thể có xuất hiện trong một chuỗi hay không, cách chính xác để kiểm tra là kiểm tra xem giá trị trả về có phải là
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
9 hay không"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
4ví dụ
Sử dụng chỉ mụcOf[]
Ví dụ sau sử dụng
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
7 để định vị các chuỗi con trong chuỗi "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
41"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
6indexOf[] và phân biệt chữ hoa chữ thường
Ví dụ sau định nghĩa hai biến chuỗi
Các biến chứa cùng một chuỗi, ngoại trừ chuỗi thứ hai chứa các chữ cái viết hoa. Phương thức
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
42 đầu tiên hiển thị "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
43. Nhưng vì phương thức "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
7 phân biệt chữ hoa chữ thường, chuỗi "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
45 không được tìm thấy trong "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
46, vì vậy phương thức "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
42 thứ hai hiển thị "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
9"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
3Sử dụng indexOf[] để đếm số lần xuất hiện của một chữ cái trong chuỗi
Ví dụ sau đặt
"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
49 thành số lần xuất hiện của chữ cái "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
60 trong chuỗi "hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
61