Lấy phần tử trong chuỗi javascript

Để lấy ba ký tự đầu tiên của một chuỗi, hãy gọi 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 và truyền vào đó các tham số là
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
8 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. 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
0 sẽ trả về một chuỗi mới chứa ba ký tự đầu tiên của chuỗi ban đầu

Hai tham số chúng ta truyền vào String. phương pháp chuỗi con là

  1. chỉ mục bắt đầu - chỉ mục của ký tự đầu tiên được đưa vào chuỗi được trả về
  2. chỉ mục kết thúc - đi tới, nhưng không bao gồm chỉ mục này

Các chỉ mục JavaScript không dựa trên. Ký tự đầu tiên trong chuỗi có chỉ số là

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
8 và ký tự cuối cùng là chỉ số 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

Để có được ba ký tự đầu tiên, chúng tôi đã chỉ định chỉ mục bắt đầu là

"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
8 và chỉ mục kết thúc 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. Điều này trích xuất các ký tự tại các 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
8,
"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à
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
80

Một cách tiếp cận khác là sử dụng Chuỗi. phương pháp lát

Sử dụng 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
81 để lấy ba ký tự đầu tiên của một chuỗi, e. g.
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
82. 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
83 sẽ trả về một chuỗi mới chứa ba ký tự đầu tiên của chuỗi ban đầu

Chúng tôi đã chuyển các tham số tương tự cho 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
83 - 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
85 và
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8
86. Tuy nhiên, các phương pháp không giống nhau

Đối với mục đích của chúng tôi, các 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
83 và
"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ũng làm như vậy. Tuy nhiên, có một số khác biệt giữa hai

Nếu bạn muốn đọc thêm về điều đó, hãy xem phần này trong tài liệu MDN

Bạn chọn cách tiếp cận nào là vấn đề sở thích cá nhân. Tôi sẽ sử dụng 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
89 vì tôi thấy phương pháp này trực quan hơn cho các trường hợp sử dụng khác được đề cập trong tài liệu MDN

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

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
0

Chuỗ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ọ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ả

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

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

Kiể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
4

ví 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
6

indexOf() 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
3

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

Bạn có thể lập chỉ mục thành một chuỗi trong JavaScript không?

Các ký tự trong chuỗi được lập chỉ mục từ trái sang phải . Chỉ mục của ký tự đầu tiên là 0 và chỉ mục của ký tự cuối cùng—trong một chuỗi có tên là Tên chuỗi là Tên chuỗi. chiều dài - 1. Nếu chỉ mục bạn cung cấp nằm ngoài phạm vi này, JavaScript sẽ trả về một chuỗi trống.

Là chuỗi con trong JavaScript?

Phương thức substring() trích xuất các ký tự, giữa hai chỉ số (vị trí), từ một chuỗi và trả về chuỗi con . Phương thức substring() trích xuất các ký tự từ đầu đến cuối (độc quyền).

Công dụng của phương thức charAt() là gì?

Phương thức charAt() trả về ký tự tại chỉ mục đã chỉ định trong một chuỗi . Chỉ số của ký tự đầu tiên là 0, ký tự thứ hai là 1, v.v.

Sự khác biệt giữa chuỗi con và chuỗi con trong JavaScript là gì?

Sự khác nhau giữa chuỗi con() và chuỗi con() . chỉ mục bắt đầu của substr() sẽ quấn đến cuối chuỗi nếu nó là số âm, trong khi substring() sẽ kẹp nó thành 0. The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .