Làm cách nào để bạn tìm thấy lần xuất hiện đầu tiên của một ký tự trong chuỗi trong javascript?

Trong bài viết này, chúng ta sẽ tìm hiểu cách tìm sự xuất hiện đầu tiên của một từ cụ thể trong một chuỗi nhất định bằng cách sử dụng các bộ lọc trong VueJS. Vue là một framework tiến bộ để xây dựng giao diện người dùng. Bộ lọc là một chức năng được cung cấp bởi các thành phần Vue cho phép bạn áp dụng định dạng và chuyển đổi cho bất kỳ phần nào của dữ liệu động mẫu của bạn. Thuộc tính bộ lọc của thành phần là một đối tượng. Một bộ lọc đơn là một hàm chấp nhận một giá trị và trả về một giá trị khác. Giá trị được trả về là giá trị thực sự được in trong Vue. mẫu js

Lần xuất hiện đầu tiên của một từ cụ thể có thể được tìm ra bằng cách áp dụng bộ lọc cho chuỗi được yêu cầu. Chúng ta sẽ sử dụng phương thức JavaScript indexOf[] để kiểm tra chỉ mục của lần xuất hiện đầu tiên của từ. Nếu từ khớp chính xác, thì chỉ mục tương ứng được trả về và nếu không tìm thấy từ thì '-1' được trả về dưới dạng chỉ mục

Để lấy chỉ mục đầu tiên về sự xuất hiện của giá trị đã chỉ định trong một chuỗi, hãy sử dụng phương thức JavaScript indexOf[]

Ví dụ

Bạn có thể thử chạy đoạn mã sau để lấy chỉ mục đầu tiên -

Bản thử trực tiếp

   
      JavaScript String indexOf[] Method
   

   
      
   

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
8, đư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, 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ị 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 — 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
indexOf[searchString]
indexOf[searchString, position]
31

indexOf[searchString]
indexOf[searchString, position]
32 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

indexOf[searchString]
indexOf[searchString, position]
32, 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
indexOf[searchString]
indexOf[searchString, position]
32 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
indexOf[searchString]
indexOf[searchString, position]
32 nhỏ hơn 0, phương thức sẽ hoạt động như nếu
indexOf[searchString]
indexOf[searchString, position]
32 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

  • indexOf[searchString]
    indexOf[searchString, position]
    
    39 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
    
    20 — 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
    
    22 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
    
    20
  • "hello world".indexOf[""]; // returns 0
    "hello world".indexOf["", 0]; // returns 0
    "hello world".indexOf["", 3]; // returns 3
    "hello world".indexOf["", 8]; // returns 8
    
    25 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
    
    27 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
    
    28, 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
    
    29
  • "hello world".indexOf[""]; // returns 0
    "hello world".indexOf["", 0]; // returns 0
    "hello world".indexOf["", 3]; // returns 3
    "hello world".indexOf["", 8]; // returns 8
    
    20 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
    
    22 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
    
    23, 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

indexOf[searchString]
indexOf[searchString, position]
3

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ự miêu 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
2

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

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

"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
5

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
8 để đị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
51

"hello world".indexOf[""]; // returns 0
"hello world".indexOf["", 0]; // returns 0
"hello world".indexOf["", 3]; // returns 3
"hello world".indexOf["", 8]; // returns 8
7

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
52 đầ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
53. 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
8 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
55 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
56, 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
52 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
4

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
59 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
70 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
71

Làm cách nào để tìm các lần xuất hiện của một ký tự trong chuỗi trong JavaScript?

Ví dụ 2. Kiểm tra sự xuất hiện của một ký tự bằng Regex . const re = new RegExp[letter, 'g']; tạo một biểu thức chính quy. Phương thức match[] trả về một mảng chứa tất cả các kết quả khớp. Đây, str.

Làm cách nào để thay thế lần xuất hiện đầu tiên của một ký tự trong chuỗi 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 lần xuất hiện đầu tiên của ký tự khỏi chuỗi trong JavaScript?

Có ba cách trong JavaScript để xóa ký tự đầu tiên khỏi chuỗi. .
Sử dụng phương thức chuỗi con[]. Phương thức substring[] trả về một phần của chuỗi giữa các chỉ mục đã chỉ định hoặc đến cuối chuỗi. .
Sử dụng phương thức slice[]. .
Sử dụng phương thức substr[]

Làm cách nào tôi có thể nhận các ký tự đầu tiên của chuỗi?

Cách lấy ký tự đầu tiên của chuỗi .
Phương thức charAt[]. Bạn nên sử dụng phương thức charAt[] tại chỉ số 0 để chọn ký tự đầu tiên của chuỗi. .
Phương thức chuỗi con[]. Bạn cũng có thể sử dụng phương thức substring[] để lấy ký tự đầu tiên. .
Phương thức slice[]. .
Ký hiệu ngoặc [] Phương pháp

Chủ Đề