Phương thức
greet='Hello World!'
print['Index of H: ', greet.index['H']]
print['Index of e: ', greet.index['e']]
print['Index of l: ', greet.index['l']]
print['Index of World: ', greet.index['World']]
1 trả về chỉ số của lần xuất hiện đầu tiên của một chuỗi con trong chuỗi đã cho. Nó giống như phương thức find[] ngoại trừ nếu không tìm thấy một chuỗi con, thì nó sẽ tạo ra một ngoại lệcú pháp
str.index[substr, start, end]
Thông số
- chất nền. [Bắt buộc] Chuỗi con có chỉ mục phải được tìm thấy
- bắt đầu. [Tùy chọn] Vị trí chỉ mục bắt đầu từ nơi bắt đầu tìm kiếm trong chuỗi. Mặc định là 0
- kết thúc. [Tùy chọn] Vị trí chỉ mục kết thúc cho đến khi quá trình tìm kiếm diễn ra. Mặc định là cuối chuỗi
Giá trị trả về
Một giá trị số nguyên cho biết chỉ mục của chuỗi con đã chỉ định
Các ví dụ sau minh họa phương pháp
greet='Hello World!'
print['Index of H: ', greet.index['H']]
print['Index of e: ', greet.index['e']]
print['Index of l: ', greet.index['l']]
print['Index of World: ', greet.index['World']]
1Thí dụ. mục lục[]
Sao chépgreet='Hello World!'
print['Index of H: ', greet.index['H']]
print['Index of e: ', greet.index['e']]
print['Index of l: ', greet.index['l']]
print['Index of World: ', greet.index['World']]
đầu ra
Index of H: 0
Index of e: 1
Index of l: 2
Index of World: 6
Phương thức
greet='Hello World!'
print['Index of H: ', greet.index['H']]
print['Index of e: ', greet.index['e']]
print['Index of l: ', greet.index['l']]
print['Index of World: ', greet.index['World']]
1 chỉ trả về một chỉ mục của lần xuất hiện đầu tiênThí dụ. index[] trả về chỉ số xuất hiện đầu tiên
Sao chépgreet='Hello World'
print['Index of l: ', greet.index['l']]
print['Index of o: ', greet.index['o']]
đầu ra
Index of l: 2
Index of o: 4
Phương pháp
greet='Hello World!'
print['Index of H: ', greet.index['H']]
print['Index of e: ', greet.index['e']]
print['Index of l: ', greet.index['l']]
print['Index of World: ', greet.index['World']]
1 thực hiện tìm kiếm phân biệt chữ hoa chữ thường. Nó ném ra greet='Hello World!'
print['Index of H: ', greet.index['H']]
print['Index of e: ', greet.index['e']]
print['Index of l: ', greet.index['l']]
print['Index of World: ', greet.index['World']]
5 nếu không tìm thấy chuỗi conThí dụ. Chỉ mục phân biệt chữ hoa chữ thường [] Phương thức
Sao chépgreet='Hello World'
print[greet.index['h']] # throws error: substring not found
print[greet.index['hello']] # throws error
đầu ra
ValueError: substring not found
ValueError: substring not found
Nếu không tìm thấy một chuỗi con nhất định thì nó sẽ báo lỗi
Thí dụ. mục lục[]
Sao chépmystr='TutorialsTeacher is a free online learning website'
print[mystr.index['python']]
đầu ra
Traceback [most recent call last]
in
1 str='TutorialsTeacher is a free online learning website'
----> 2 print[str.index['python']]
ValueError: substring not found
Sử dụng tham số bắt đầu và kết thúc để giới hạn tìm kiếm chuỗi con giữa chỉ mục bắt đầu và kết thúc đã chỉ định
Phương thức indexOf[]
, đư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ỉ định
indexOf[searchString]
indexOf[searchString, position]
"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,
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ị cưỡng chế 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["", 11]; // returns 11
"hello world".indexOf["", 13]; // returns 11
"hello world".indexOf["", 22]; // returns 11
1"hello world".indexOf["", 11]; // returns 11
"hello world".indexOf["", 13]; // returns 11
"hello world".indexOf["", 22]; // returns 11
2 Tùy chọnPhương thức này trả về chỉ mục của lần xuất hiện đầu tiên của chuỗi con đã chỉ định ở vị trí lớn hơn hoặc bằng
"hello world".indexOf["", 11]; // returns 11
"hello world".indexOf["", 13]; // returns 11
"hello world".indexOf["", 22]; // returns 11
2, 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["", 11]; // returns 11
"hello world".indexOf["", 13]; // returns 11
"hello world".indexOf["", 22]; // returns 11
2 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["", 11]; // returns 11
"hello world".indexOf["", 13]; // returns 11
"hello world".indexOf["", 22]; // returns 11
2 nhỏ hơn 0, phương thức sẽ hoạt động như nếu "hello world".indexOf["", 11]; // returns 11
"hello world".indexOf["", 13]; // returns 11
"hello world".indexOf["", 22]; // returns 11
2 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
9 trả về"hello world".indexOf["", 11]; // returns 11 "hello world".indexOf["", 13]; // returns 11 "hello world".indexOf["", 22]; // returns 11
0 — bởi vì nó khiến phương thức hoạt động như thể đối số thứ hai là"Blue Whale".indexOf["Blue"]; // returns 0 "Blue Whale".indexOf["Blute"]; // returns -1 "Blue Whale".indexOf["Whale", 0]; // returns 5 "Blue Whale".indexOf["Whale", 5]; // returns 5 "Blue Whale".indexOf["Whale", 7]; // returns -1 "Blue Whale".indexOf[""]; // returns 0 "Blue Whale".indexOf["", 9]; // returns 9 "Blue Whale".indexOf["", 10]; // returns 10 "Blue Whale".indexOf["", 11]; // returns 10
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
2 ở vị trí lớn hơn hoặc bằng"Blue Whale".indexOf["Blue"]; // returns 0 "Blue Whale".indexOf["Blute"]; // returns -1 "Blue Whale".indexOf["Whale", 0]; // returns 5 "Blue Whale".indexOf["Whale", 5]; // returns 5 "Blue Whale".indexOf["Whale", 7]; // returns -1 "Blue Whale".indexOf[""]; // returns 0 "Blue Whale".indexOf["", 9]; // returns 9 "Blue Whale".indexOf["", 10]; // returns 10 "Blue Whale".indexOf["", 11]; // returns 10
4 là ở 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
0"Blue Whale".indexOf["Blue"]; // returns 0 "Blue Whale".indexOf["Blute"]; // returns -1 "Blue Whale".indexOf["Whale", 0]; // returns 5 "Blue Whale".indexOf["Whale", 5]; // returns 5 "Blue Whale".indexOf["Whale", 7]; // returns -1 "Blue Whale".indexOf[""]; // returns 0 "Blue Whale".indexOf["", 9]; // returns 9 "Blue Whale".indexOf["", 10]; // returns 10 "Blue Whale".indexOf["", 11]; // returns 10
5 trả về"Blue Whale".indexOf["Blue"]; // returns 0 "Blue Whale".indexOf["Blute"]; // returns -1 "Blue Whale".indexOf["Whale", 0]; // returns 5 "Blue Whale".indexOf["Whale", 5]; // returns 5 "Blue Whale".indexOf["Whale", 7]; // returns -1 "Blue Whale".indexOf[""]; // returns 0 "Blue Whale".indexOf["", 9]; // returns 9 "Blue Whale".indexOf["", 10]; // returns 10 "Blue Whale".indexOf["", 11]; // returns 10
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
7 xuất hiện tại chỉ mục"Blue Whale".indexOf["Blue"]; // returns 0 "Blue Whale".indexOf["Blute"]; // returns -1 "Blue Whale".indexOf["Whale", 0]; // returns 5 "Blue Whale".indexOf["Whale", 5]; // returns 5 "Blue Whale".indexOf["Whale", 7]; // returns -1 "Blue Whale".indexOf[""]; // returns 0 "Blue Whale".indexOf["", 9]; // returns 9 "Blue Whale".indexOf["", 10]; // returns 10 "Blue Whale".indexOf["", 11]; // returns 10
8, nhưng vị trí đó không lớn hơn hoặc bằng"Blue Whale".indexOf["Blue"]; // returns 0 "Blue Whale".indexOf["Blute"]; // returns -1 "Blue Whale".indexOf["Whale", 0]; // returns 5 "Blue Whale".indexOf["Whale", 5]; // returns 5 "Blue Whale".indexOf["Whale", 7]; // returns -1 "Blue Whale".indexOf[""]; // returns 0 "Blue Whale".indexOf["", 9]; // returns 9 "Blue Whale".indexOf["", 10]; // returns 10 "Blue Whale".indexOf["", 11]; // returns 10
9"Blue Whale".indexOf["Blue"]; // returns 0 "Blue Whale".indexOf["Blute"]; // returns -1 "Blue Whale".indexOf["Whale", 0]; // returns 5 "Blue Whale".indexOf["Whale", 5]; // returns 5 "Blue Whale".indexOf["Whale", 7]; // returns -1 "Blue Whale".indexOf[""]; // returns 0 "Blue Whale".indexOf["", 9]; // returns 9 "Blue Whale".indexOf["", 10]; // returns 10 "Blue Whale".indexOf["", 11]; // returns 10
0 trả về"Blue Whale".indexOf["blue"]; // returns -1
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
2 lớn hơn độ dài của"Blue Whale".indexOf["blue"]; // returns -1
3, khiến phương thức hoàn toàn không tìm kiếm chuỗi"Blue Whale".indexOf["blue"]; // returns -1
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["", 11]; // returns 11
"hello world".indexOf["", 13]; // returns 11
"hello world".indexOf["", 22]; // returns 11
Trong 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
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"Blue Whale".indexOf["Blue"]; // returns 0
"Blue Whale".indexOf["Blute"]; // returns -1
"Blue Whale".indexOf["Whale", 0]; // returns 5
"Blue Whale".indexOf["Whale", 5]; // returns 5
"Blue Whale".indexOf["Whale", 7]; // returns -1
"Blue Whale".indexOf[""]; // returns 0
"Blue Whale".indexOf["", 9]; // returns 9
"Blue Whale".indexOf["", 10]; // returns 10
"Blue Whale".indexOf["", 11]; // returns 10
Phương pháp indexOf[]
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"Blue Whale".indexOf["blue"]; // returns -1
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"Blue Whale".indexOf["Blue"] !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf["Bloe"] !== -1; // false; no 'Bloe' in 'Blue Whale'
Ví dụ sau sử dụng indexOf[]
để định vị các chuỗi con trong chuỗi
"Blue Whale".indexOf["Blue"] !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf["Bloe"] !== -1; // false; no 'Bloe' in 'Blue Whale'
1const str = "Brave new world";
console.log[str.indexOf["w"]]; // 8
console.log[str.indexOf["new"]]; // 6
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
"Blue Whale".indexOf["Blue"] !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf["Bloe"] !== -1; // false; no 'Bloe' in 'Blue Whale'
2 đầu tiên hiển thị "Blue Whale".indexOf["Blue"] !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf["Bloe"] !== -1; // false; no 'Bloe' in 'Blue Whale'
3. Nhưng vì phương thức indexOf[]
phân biệt chữ hoa chữ thường, chuỗi "Blue Whale".indexOf["Blue"] !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf["Bloe"] !== -1; // false; no 'Bloe' in 'Blue Whale'
5 không được tìm thấy trong "Blue Whale".indexOf["Blue"] !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf["Bloe"] !== -1; // false; no 'Bloe' in 'Blue Whale'
6, vì vậy phương thức thứ hai "Blue Whale".indexOf["Blue"] !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf["Bloe"] !== -1; // false; no 'Bloe' in 'Blue Whale'
2 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
9const myString = "brie, pepper jack, cheddar";
const myCapString = "Brie, Pepper Jack, Cheddar";
console.log[myString.indexOf["cheddar"]]; // 19
console.log[myCapString.indexOf["cheddar"]]; // -1
Ví dụ sau đặt
"Blue Whale".indexOf["Blue"] !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf["Bloe"] !== -1; // false; no 'Bloe' in 'Blue Whale'
9 thành số lần xuất hiện của chữ cái const str = "Brave new world";
console.log[str.indexOf["w"]]; // 8
console.log[str.indexOf["new"]]; // 6
0 trong chuỗi const str = "Brave new world";
console.log[str.indexOf["w"]]; // 8
console.log[str.indexOf["new"]]; // 6
1