Kiểm tra xem ký tự đầu tiên của chuỗi có phải là dấu cách không JavaScript

Phương thức charAt[] của đối tượng String trả về một chuỗi mới bao gồm đơn vị mã UTF-16 duy nhất nằm ở phần bù được chỉ định trong chuỗi

Thử nó

cú pháp

charAt[index]

Thông số

const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
0

Một số nguyên giữa

const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
1 và
const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
2. Nếu không thể chuyển đổi
const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
0 thành số nguyên hoặc không cung cấp
const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
0, giá trị mặc định là
const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
1, do đó, ký tự đầu tiên của
const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
3 được trả về

Giá trị trả về

Một chuỗi đại diện cho ký tự [chính xác là một đơn vị mã UTF-16] tại

const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
0 đã chỉ định. Nếu
const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
0 nằm ngoài phạm vi, charAt[] trả về một chuỗi rỗng

Sự miêu tả

Các ký tự trong một 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à

const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
1 và chỉ mục của ký tự cuối cùng—trong một chuỗi có tên là
const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
8 là
const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
9. Nếu
const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
0 bạn cung cấp nằm ngoài phạm vi này, JavaScript sẽ trả về một chuỗi trống

Nếu không cung cấp

const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
0 cho charAt[], thì mặc định là
const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];
1

ví dụ

Hiển thị các ký tự ở các vị trí khác nhau trong một chuỗi

Ví dụ sau hiển thị các ký tự ở các vị trí khác nhau trong chuỗi "______44"

const anyString = "Brave new world";
console.log[`The character at index 0   is '${anyString.charAt[]}'`];
// No index was provided, used 0 as default

console.log[`The character at index 0   is '${anyString.charAt[0]}'`];
console.log[`The character at index 1   is '${anyString.charAt[1]}'`];
console.log[`The character at index 2   is '${anyString.charAt[2]}'`];
console.log[`The character at index 3   is '${anyString.charAt[3]}'`];
console.log[`The character at index 4   is '${anyString.charAt[4]}'`];
console.log[`The character at index 999 is '${anyString.charAt[999]}'`];

Những dòng này hiển thị như sau

The character at index 0   is 'B'

The character at index 0   is 'B'
The character at index 1   is 'r'
The character at index 2   is 'a'
The character at index 3   is 'v'
The character at index 4   is 'e'
The character at index 999 is ''

Nhận toàn bộ ký tự

Điều sau đây cung cấp một phương tiện để đảm bảo rằng việc đi qua một vòng lặp chuỗi luôn cung cấp toàn bộ ký tự, ngay cả khi chuỗi chứa các ký tự không có trong Mặt phẳng đa ngôn ngữ cơ bản

const str = "A\uD87E\uDC04Z"; // We could also use a non-BMP character directly
for [let i = 0; i 

Chủ Đề