Ví dụ
Tìm kiếm một chuỗi cho "Chào mừng":
hãy để văn bản = "Xin chào thế giới, chào mừng bạn đến với vũ trụ."; Đặt kết quả = text.indexof ["Chào mừng"];
let result = text.indexOf["welcome"];
Hãy tự mình thử »
Tìm kiếm một chuỗi cho "Chào mừng":
hãy để văn bản = "Xin chào thế giới, chào mừng bạn đến với vũ trụ."; Đặt kết quả = text.indexof ["Chào mừng"];
let result = text.indexOf["Welcome"];
Hãy tự mình thử »
Tìm kiếm một chuỗi cho "Chào mừng":
hãy để văn bản = "Xin chào thế giới, chào mừng bạn đến với vũ trụ."; Đặt kết quả = text.indexof ["Chào mừng"];
text.indexOf["e"];
Hãy tự mình thử »
Tìm kiếm một chuỗi cho "Chào mừng":
hãy để văn bản = "Xin chào thế giới, chào mừng bạn đến với vũ trụ."; Đặt kết quả = text.indexof ["Chào mừng"];
text.indexOf["e", 5];
Hãy tự mình thử »
Tìm kiếm một chuỗi cho "Chào mừng":
hãy để văn bản = "Xin chào thế giới, chào mừng bạn đến với vũ trụ."; Đặt kết quả = text.indexof ["Chào mừng"];
text.indexOf["a"];
Hãy tự mình thử »
Tìm kiếm một chuỗi cho "Chào mừng":
hãy để văn bản = "Xin chào thế giới, chào mừng bạn đến với vũ trụ."; Đặt kết quả = text.indexof ["Chào mừng"];
Tìm sự xuất hiện đầu tiên của "E":
hãy để văn bản = "Xin chào thế giới, chào mừng bạn đến với vũ trụ."; text.indexof ["e"];
Tìm lần xuất hiện đầu tiên của "E", bắt đầu từ vị trí 5:
hãy để văn bản = "Xin chào thế giới, chào mừng bạn đến với vũ trụ."; Text.indexof ["E", 5];
Tìm sự xuất hiện đầu tiên của "A":
hãy để văn bản = "Xin chào thế giới, chào mừng bạn đến với vũ trụ."; Text.indexof ["A"]; | Định nghĩa và cách sử dụng |
Phương thức 0 trả về vị trí của lần xuất hiện đầu tiên của một giá trị trong một chuỗi. | Phương thức 0 trả về -1 nếu không tìm thấy giá trị.The string to search for. |
Phương pháp 0 là trường hợp nhạy cảm. | Cú pháp The position to start from [default is 0]. |
String.indexof [searchValue, bắt đầu]
Thông số | Định nghĩa và cách sử dụng |
Phương thức 0 trả về vị trí của lần xuất hiện đầu tiên của một giá trị trong một chuỗi. | Phương thức 0 trả về -1 nếu không tìm thấy giá trị.-1 if it never occurs. |
Phương pháp indexOf[searchString]
indexOf[searchString, position]
0 là trường hợp nhạy cảm.
String indexOf[] and String search[]
indexOf[searchString]
indexOf[searchString, position]
Cú pháp
String.indexof [searchValue, bắt đầu]
Thông số
Tham số
Sự mô tả
SearchValue | Yêu cầu. Chuỗi để tìm kiếm. | bắt đầu | Tùy chọn. Vị trí để bắt đầu từ [mặc định là 0]. | Giá trị trả về | Loại hình |
Một số | Một số | Một số | Một số | Một số | Một số |
Giả sử tôi có một chuỗi, nhưng văn bản có thể là bất cứ điều gì!
Đây là một trường hợp thử nghiệm
'Ranh giới' là số đếm sau một không gian, điều này có nghĩa là charindex =
[0] Điều này [5] là [8] A [10] Kiểm tra [15] Trường hợp
Phương tiện trên có nghĩa là từ [0] đến [5] từ này bằng 'cái này'.
Nếu tôi muốn biết từ vừa được sử dụng, tôi sẽ cần phải bắt đầu
indexOf[searchString]
indexOf[searchString, position]
6 tại indexOf[searchString]
indexOf[searchString, position]
7 và nhìn ngược cho đến khi indexOf[searchString]
indexOf[searchString, position]
8Ví dụ: Nếu tôi muốn tìm từ chỉ được sử dụng tại Charindex [10] Tôi sẽ hướng dẫn JavaScript nhìn về phía sau cho đến không gian tiếp theo và cho tôi từ "A".
Có thể làm điều này, với ít thời gian quá trình, sử dụng JavaScript hoặc jQuery?
CẬP NHẬT
Thay vì chăm sóc từ và quay lại, tôi quyết định đạt được những gì tôi cần, tôi chia các từ và sử dụng mỗi charindex như một điểm. Mã theo sau:
var u = new SpeechSynthesisUtterance[];
var words = element.innerHTML.split[' '];
var a = 0;
u.text = element.innerHTML;
u.lang = 'en-UK';
u.rate = 0.7;
u.onboundary = function[event] {
console.log[words];
element.innerHTML = element.innerHTML.replace[words[a], '' + words[a] + ''];
a++;
}
Nhưng vì đây không phải là câu trả lời câu hỏi, vẫn là câu trả lời tốt nhất để tìm kiếm ngược một chuỗi là câu hỏi được đánh dấu chính xác, mặc dù nó vẫn cần một số công việc tùy thuộc vào cấu trúc chuỗi.
Phương thức
0, được đư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 được chỉ định. Đưa ra một đối số thứ hai: một số, phương thức trả về sự xuất hiện đầu tiên của chuỗi con được chỉ định tại một chỉ mục lớn hơn hoặc bằng số được chỉ định.indexOf[searchString]
indexOf[searchString, position]
0 method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the first occurrence of the specified substring. Given a second argument: a number, the method returns the first occurrence of the specified substring at an index greater than or equal to the specified number.indexOf[searchString]
indexOf[searchString, position]
Thử 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
0Subring để tìm kiếm, ép buộc vào một chuỗi.
Nếu phương thức được gọi không có đối số,
'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 buộc 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 đó, ________ 23 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 - bởi vì không tìm thấy 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 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ọnOptionalPhương thức trả về chỉ số của lần xuất hiện đầu tiên của chuỗi con được chỉ định tại một 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, phương thức 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 pháp này 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 pháp 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 ở 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ì, trong khi đó đú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 xảy ra ở 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, 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 - bởi 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, điều này khiến phương pháp không tìm kiếm chuỗi nào cả.'Blue Whale'.indexOf['blue'] // returns -1
Giá trị trả về
Chỉ số của 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ấy.Trả 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 với đối số thứ hai có giá trị nhỏ hơn độ dài của chuỗi cuộc gọi, giá trị trả về giống như 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 chiều 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 trường hợp trước đây, phương thức hoạt động như thể nó tìm thấy một chuỗi trống ngay sau khi vị trí được chỉ định trong đối số thứ hai. Trong trường hợp sau, 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 là không chỉ số: 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ừ 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[searchString]
indexOf[searchString, position]
0 là trường hợp nhạy cảm. 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
Kiểm tra sự xuất hiện
Khi kiểm tra xem một chuỗi con cụ thể xảy ra trong một chuỗi, cách chính xác để kiểm tra xem có 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ụ
Sử dụng indexof []
Ví dụ sau sử dụng
indexOf[searchString]
indexOf[searchString, position]
0 để đị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'
1.const str = 'Brave new world';
console.log[str.indexOf['w']]; // 8
console.log[str.indexOf['new']]; // 6
indexof [] và độ nhạy trường hợp
Ví dụ sau đây xác định 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 chữ 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 pháp indexOf[searchString]
indexOf[searchString, position]
0 nhạy cảm trường hợp, 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, do đó 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 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.const myString = 'brie, pepper jack, cheddar';
const myCapString = 'Brie, Pepper Jack, Cheddar';
console.log[myString.indexOf['cheddar']]; // 19
console.log[myCapString.indexOf['cheddar']]; // -1
Sử dụng indexof [] để đếm các lần xuất hiện của một chữ cái trong một chuỗi
Ví dụ sau đây đặ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:const str = 'To be, or not to be, that is the question.';
let count = 0;
let position = str.indexOf['e'];
while [position !== -1] {
count++;
position = str.indexOf['e', position + 1];
}
console.log[count]; // 4
Thông số kỹ thuật
Thông số kỹ thuật ngôn ngữ Ecmascript # sec-string.prototype.indexof # sec-string.prototype.indexof |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt