Viết hàm JavaScript để loại bỏ lần xuất hiện đầu tiên của một chuỗi tìm kiếm đã cho khỏi một chuỗi

ví dụ

Tìm kiếm một chuỗi cho "chào mừng"

let text = "Xin chào thế giới, chào mừng đến với vũ trụ. “;
để kết quả = văn bản. indexOf("chào mừng");

Tự mình thử »

Tìm kiếm một chuỗi cho "Chào mừng"

let text = "Xin chào thế giới, chào mừng đến với vũ trụ. “;
để kết quả = văn bản. indexOf("Chào mừng");

Tự mình thử »

Tìm lần xuất hiện đầu tiên của "e"

let text = "Xin chào thế giới, chào mừng đến với vũ trụ. “;
chữ. indexOf("e");

Tự mình thử »

Tìm lần xuất hiện đầu tiên của "e", bắt đầu từ vị trí 5

let text = "Xin chào thế giới, chào mừng đến với vũ trụ. “;
chữ. indexOf("e", 5);

Tự mình thử »

Tìm lần xuất hiện đầu tiên của "a"

let text = "Xin chào thế giới, chào mừng đến với vũ trụ. “;
chữ. indexOf("a");

Tự mình thử »


Định nghĩa và cách sử dụng

Phương thức indexOf() trả về vị trí xuất hiện đầu tiên của một giá trị trong chuỗi

Phương thức indexOf() trả về -1 nếu không tìm thấy giá trị

Phương pháp indexOf() phân biệt chữ hoa chữ thường


cú pháp

chuỗi. indexOf(giá trị tìm kiếm, bắt đầu)

Thông số

Tham sốMô tảgiá trị tìm kiếmBắt buộc
Chuỗi để tìm kiếm. bắt đầuTùy chọn
Vị trí bắt đầu từ (mặc định là 0)

Giá trị trả về

LoạiMô tảMột sốVị trí đầu tiên xuất hiện giá trị tìm kiếm
-1 nếu nó không bao giờ xảy ra

sự khác biệt giữaChuỗi indexOf() và Chuỗi tìm kiếm()

Phương thức indexOf() không thể tìm kiếm theo biểu thức chính quy

search() không thể lấy đối số vị trí bắt đầu



Hỗ trợ trình duyệt

indexOf() là một tính năng ECMAScript1 (ES1)

ES1 (JavaScript 1997) được hỗ trợ đầy đủ trên mọi trình duyệt

ChromeEdgeFirefoxSafariOperaIECóCóCóCóCóCó

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
0

Chuỗi con để tìm kiếm. Tất cả các giá trị đều là , vì vậy việc bỏ qua nó hoặc chuyển qua

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
1 sẽ khiến indexOf() tìm kiếm 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
3, đây hiếm khi là điều bạn muốn

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
4 Tùy chọn

Phươ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 ở 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, 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
6. 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
4 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
4 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
4 là
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
6

  • "hello world".indexOf("", 11); // returns 11
    "hello world".indexOf("", 13); // returns 11
    "hello world".indexOf("", 22); // returns 11
    
    1 trả về
    "hello world".indexOf("", 11); // returns 11
    "hello world".indexOf("", 13); // returns 11
    "hello world".indexOf("", 22); // returns 11
    
    2 — 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
    
    6 và lần xuất hiện đầu tiên của
    "hello world".indexOf("", 11); // returns 11
    "hello world".indexOf("", 13); // returns 11
    "hello world".indexOf("", 22); // returns 11
    
    4 ở 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
    
    6 là ở vị trí
    "hello world".indexOf("", 11); // returns 11
    "hello world".indexOf("", 13); // returns 11
    "hello world".indexOf("", 22); // returns 11
    
    2
  • "hello world".indexOf("", 11); // returns 11
    "hello world".indexOf("", 13); // returns 11
    "hello world".indexOf("", 22); // returns 11
    
    7 trả về
    "hello world".indexOf("", 11); // returns 11
    "hello world".indexOf("", 13); // returns 11
    "hello world".indexOf("", 22); // returns 11
    
    8 — bởi vì, mặc dù đúng là chuỗi con
    "hello world".indexOf("", 11); // returns 11
    "hello world".indexOf("", 13); // returns 11
    "hello world".indexOf("", 22); // returns 11
    
    9 xảy ra 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
    
    0, 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
    
    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
    
    2 trả về
    "hello world".indexOf("", 11); // returns 11
    "hello world".indexOf("", 13); // returns 11
    "hello world".indexOf("", 22); // returns 11
    
    8 — 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
    
    4 lớn hơn độ dài của
    "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, khiến phương thức hoàn toàn không tìm kiếm chuỗi

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("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11
8 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 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ỉ số 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
6 và chỉ số 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("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11
8

"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("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11
8

"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"); // returns -1
3

const 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"); // returns -1
4 đầu tiên hiển thị
"Blue Whale".indexOf("blue"); // returns -1
5. Nhưng vì phương thức indexOf() phân biệt chữ hoa chữ thường, chuỗi
"Blue Whale".indexOf("blue"); // returns -1
7 không được tìm thấy trong
"Blue Whale".indexOf("blue"); // returns -1
8, vì vậy phương thức
"Blue Whale".indexOf("blue"); // returns -1
4 thứ hai hiển thị
"hello world".indexOf("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11
8

const 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'
1 thành số lần xuất hiện của chữ cái
"Blue Whale".indexOf("Blue") !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf("Bloe") !== -1; // false; no 'Bloe' in 'Blue Whale'
2 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'
3

Làm cách nào để xóa phần tử đầu tiên khỏi chuỗi trong JavaScript?

Phương pháp 1. Sử dụng phương thức slice() . Phương thức slice() trích xuất một phần của chuỗi và trả về phần được trích xuất trong một chuỗi mới. Nếu chúng ta muốn xóa ký tự đầu tiên của chuỗi thì có thể thực hiện bằng cách chỉ định chỉ mục bắt đầu mà chuỗi cần được trích xuất. Chúng ta cũng có thể cắt phần tử cuối cùng.

Làm cách nào để xóa lần xuất hiện đầu tiên của ký tự khỏi chuỗi trong JavaScript?

Sử dụng phương thức replace() để thay thế lần xuất hiện đầu tiên của ký tự trong chuỗi. Phương thức lấy một biểu thức chính quy và một chuỗi thay thế làm tham số và trả về một chuỗi mới với một hoặc nhiều kết quả được thay thế.

Làm cách nào để xóa từ đầu tiên khỏi chuỗi trong JavaScript?

tách(' '); . shift(); // các phần được sửa đổi để loại bỏ từ đầu tiên var result; . tham gia(' '); . Lưu câu trả lời này.

Làm cách nào để xóa một phần chuỗi khỏi chuỗi trong JavaScript?

Phương thức replace() là một trong những kỹ thuật được sử dụng phổ biến nhất để xóa ký tự khỏi chuỗi trong javascript. Phương thức replace() nhận hai tham số, tham số đầu tiên là ký tự được thay thế và tham số thứ hai là ký tự cần thay thế bằng.