Chuỗi thay thế tại chỉ mục JavaScript

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ỉ đị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,

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ọ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 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

  • "hello world".indexOf(""); // returns 0
    "hello world".indexOf("", 0); // returns 0
    "hello world".indexOf("", 3); // returns 3
    "hello world".indexOf("", 8); // returns 8
    
    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ả

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ấ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(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
2

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

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
1

Phươ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
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 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
4

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
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

"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
3

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

Làm cách nào để thay thế ký tự chuỗi tại chỉ mục trong JavaScript?

Phương pháp đầu tiên là bằng cách sử dụng phương thức substr() . Và ở cách thứ 2, chúng ta sẽ chuyển chuỗi thành mảng và thay thế ký tự tại chỉ mục. Cả hai phương pháp được mô tả dưới đây. Sử dụng phương thức substr(). Phương thức substr() được sử dụng để trích xuất một chuỗi con từ một chỉ mục bắt đầu cho trước sang một chỉ mục khác.

Làm cách nào để thay thế một chuỗi tại một vị trí cụ thể trong JavaScript?

Thay thế ký tự tại chỉ mục cụ thể trong JavaScript .
Chuỗi. nguyên mẫu. thayAt = chức năng (chỉ mục, thay thế) {
nếu (chỉ số >= cái này. chiều dài) {
trả lại cái này. giá trị của();
trả lại cái này. chuỗi con (0, chỉ mục) + thay thế + này. chuỗi con(chỉ số + 1);

Làm cách nào để xóa ký tự khỏi chuỗi bằng chỉ mục trong JavaScript?

Đầu ra. Phương pháp 4. Xóa một ký tự cụ thể tại chỉ mục đã cho bằng cách sử dụng phương thức substr() của JavaScript . Phương pháp này có thể được sử dụng để xóa một ký tự khỏi một chỉ mục cụ thể trong chuỗi. Phương thức substr() được sử dụng để trích xuất các phần của chuỗi giữa các tham số đã cho.

Làm cách nào để thay thế một chuỗi sau một ký tự cụ thể trong JavaScript?

Trả lời. Sử dụng phương thức thay thế() của JavaScript . Tuy nhiên, thay thế () sẽ chỉ thay thế lần xuất hiện đầu tiên của ký tự được chỉ định. Để thay thế tất cả các lần xuất hiện, bạn có thể sử dụng công cụ sửa đổi toàn cầu ( g ).