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

Phương thức substring() trả về một phần của

const anyString = "Mozilla";

console.log(anyString.substring(0, 1)); // 'M'
console.log(anyString.substring(1, 0)); // 'M'

console.log(anyString.substring(0, 6)); // 'Mozill'

console.log(anyString.substring(4)); // 'lla'
console.log(anyString.substring(4, 7)); // 'lla'
console.log(anyString.substring(7, 4)); // 'lla'

console.log(anyString.substring(0, 7)); // 'Mozilla'
console.log(anyString.substring(0, 10)); // 'Mozilla'
0 từ chỉ mục bắt đầu cho đến và không bao gồm chỉ mục kết thúc hoặc đến cuối chuỗi nếu không có chỉ mục kết thúc nào được cung cấp

substring(indexStart)
substring(indexStart, indexEnd)

const anyString = "Mozilla";

console.log(anyString.substring(0, 1)); // 'M'
console.log(anyString.substring(1, 0)); // 'M'

console.log(anyString.substring(0, 6)); // 'Mozill'

console.log(anyString.substring(4)); // 'lla'
console.log(anyString.substring(4, 7)); // 'lla'
console.log(anyString.substring(7, 4)); // 'lla'

console.log(anyString.substring(0, 7)); // 'Mozilla'
console.log(anyString.substring(0, 10)); // 'Mozilla'
1

Chỉ mục của ký tự đầu tiên bao gồm trong chuỗi con được trả về

const anyString = "Mozilla";

console.log(anyString.substring(0, 1)); // 'M'
console.log(anyString.substring(1, 0)); // 'M'

console.log(anyString.substring(0, 6)); // 'Mozill'

console.log(anyString.substring(4)); // 'lla'
console.log(anyString.substring(4, 7)); // 'lla'
console.log(anyString.substring(7, 4)); // 'lla'

console.log(anyString.substring(0, 7)); // 'Mozilla'
console.log(anyString.substring(0, 10)); // 'Mozilla'
2 Tùy chọn

Chỉ mục của ký tự đầu tiên cần loại trừ khỏi chuỗi con được trả về

Một chuỗi mới chứa phần được chỉ định của chuỗi đã cho

substring() trích xuất các ký tự từ

const anyString = "Mozilla";

console.log(anyString.substring(0, 1)); // 'M'
console.log(anyString.substring(1, 0)); // 'M'

console.log(anyString.substring(0, 6)); // 'Mozill'

console.log(anyString.substring(4)); // 'lla'
console.log(anyString.substring(4, 7)); // 'lla'
console.log(anyString.substring(7, 4)); // 'lla'

console.log(anyString.substring(0, 7)); // 'Mozilla'
console.log(anyString.substring(0, 10)); // 'Mozilla'
1 đến nhưng không bao gồm
const anyString = "Mozilla";

console.log(anyString.substring(0, 1)); // 'M'
console.log(anyString.substring(1, 0)); // 'M'

console.log(anyString.substring(0, 6)); // 'Mozill'

console.log(anyString.substring(4)); // 'lla'
console.log(anyString.substring(4, 7)); // 'lla'
console.log(anyString.substring(7, 4)); // 'lla'

console.log(anyString.substring(0, 7)); // 'Mozilla'
console.log(anyString.substring(0, 10)); // 'Mozilla'
2. Đặc biệt

  • Nếu
    const anyString = "Mozilla";
    
    console.log(anyString.substring(0, 1)); // 'M'
    console.log(anyString.substring(1, 0)); // 'M'
    
    console.log(anyString.substring(0, 6)); // 'Mozill'
    
    console.log(anyString.substring(4)); // 'lla'
    console.log(anyString.substring(4, 7)); // 'lla'
    console.log(anyString.substring(7, 4)); // 'lla'
    
    console.log(anyString.substring(0, 7)); // 'Mozilla'
    console.log(anyString.substring(0, 10)); // 'Mozilla'
    
    2 bị bỏ qua, substring() sẽ trích xuất các ký tự đến cuối chuỗi
  • Nếu
    const anyString = "Mozilla";
    
    console.log(anyString.substring(0, 1)); // 'M'
    console.log(anyString.substring(1, 0)); // 'M'
    
    console.log(anyString.substring(0, 6)); // 'Mozill'
    
    console.log(anyString.substring(4)); // 'lla'
    console.log(anyString.substring(4, 7)); // 'lla'
    console.log(anyString.substring(7, 4)); // 'lla'
    
    console.log(anyString.substring(0, 7)); // 'Mozilla'
    console.log(anyString.substring(0, 10)); // 'Mozilla'
    
    1 bằng với
    const anyString = "Mozilla";
    
    console.log(anyString.substring(0, 1)); // 'M'
    console.log(anyString.substring(1, 0)); // 'M'
    
    console.log(anyString.substring(0, 6)); // 'Mozill'
    
    console.log(anyString.substring(4)); // 'lla'
    console.log(anyString.substring(4, 7)); // 'lla'
    console.log(anyString.substring(7, 4)); // 'lla'
    
    console.log(anyString.substring(0, 7)); // 'Mozilla'
    console.log(anyString.substring(0, 10)); // 'Mozilla'
    
    2, thì substring() trả về một chuỗi rỗng
  • Nếu
    const anyString = "Mozilla";
    
    console.log(anyString.substring(0, 1)); // 'M'
    console.log(anyString.substring(1, 0)); // 'M'
    
    console.log(anyString.substring(0, 6)); // 'Mozill'
    
    console.log(anyString.substring(4)); // 'lla'
    console.log(anyString.substring(4, 7)); // 'lla'
    console.log(anyString.substring(7, 4)); // 'lla'
    
    console.log(anyString.substring(0, 7)); // 'Mozilla'
    console.log(anyString.substring(0, 10)); // 'Mozilla'
    
    1 lớn hơn
    const anyString = "Mozilla";
    
    console.log(anyString.substring(0, 1)); // 'M'
    console.log(anyString.substring(1, 0)); // 'M'
    
    console.log(anyString.substring(0, 6)); // 'Mozill'
    
    console.log(anyString.substring(4)); // 'lla'
    console.log(anyString.substring(4, 7)); // 'lla'
    console.log(anyString.substring(7, 4)); // 'lla'
    
    console.log(anyString.substring(0, 7)); // 'Mozilla'
    console.log(anyString.substring(0, 10)); // 'Mozilla'
    
    2, thì hiệu ứng của substring() như thể hai đối số đã được đổi chỗ cho nhau;

Bất kỳ giá trị đối số nào nhỏ hơn

const text = "Mozilla";

// Takes 4 last characters of string
console.log(text.substring(text.length - 4)); // prints "illa"

// Takes 5 last characters of string
console.log(text.substring(text.length - 5)); // prints "zilla"
4 hoặc lớn hơn
const text = "Mozilla";

// Takes 4 last characters of string
console.log(text.substring(text.length - 4)); // prints "illa"

// Takes 5 last characters of string
console.log(text.substring(text.length - 5)); // prints "zilla"
5 được xử lý như thể nó là ____11_______4 và ____11_______5, tương ứng

Bất kỳ giá trị đối số nào là

const text = "Mozilla";

// Takes 4 last characters of string
console.log(text.substring(text.length - 4)); // prints "illa"

// Takes 5 last characters of string
console.log(text.substring(text.length - 5)); // prints "zilla"
8 đều được xử lý như thể nó là
const text = "Mozilla";

// Takes 4 last characters of string
console.log(text.substring(text.length - 4)); // prints "illa"

// Takes 5 last characters of string
console.log(text.substring(text.length - 5)); // prints "zilla"
4

Ví dụ sau sử dụng substring() để hiển thị các ký tự từ chuỗi

const text = "Mozilla";
console.log(text.substring(2, 5)); // "zil"
console.log(text.substr(2, 3)); // "zil"
1

const anyString = "Mozilla";

console.log(anyString.substring(0, 1)); // 'M'
console.log(anyString.substring(1, 0)); // 'M'

console.log(anyString.substring(0, 6)); // 'Mozill'

console.log(anyString.substring(4)); // 'lla'
console.log(anyString.substring(4, 7)); // 'lla'
console.log(anyString.substring(7, 4)); // 'lla'

console.log(anyString.substring(0, 7)); // 'Mozilla'
console.log(anyString.substring(0, 10)); // 'Mozilla'

Ví dụ sau sử dụng phương thức substring() và thuộc tính

const text = "Mozilla";
console.log(text.substring(2, 5)); // "zil"
console.log(text.substr(2, 3)); // "zil"
3 để trích xuất các ký tự cuối cùng của một chuỗi cụ thể. Phương pháp này có thể dễ nhớ hơn vì bạn không cần biết chỉ số bắt đầu và chỉ số kết thúc như trong các ví dụ trên

const text = "Mozilla";

// Takes 4 last characters of string
console.log(text.substring(text.length - 4)); // prints "illa"

// Takes 5 last characters of string
console.log(text.substring(text.length - 5)); // prints "zilla"

Có sự khác biệt nhỏ giữa các phương pháp substring()

const text = "Mozilla";
console.log(text.substring(2, 5)); // "zil"
console.log(text.substr(2, 3)); // "zil"
5, vì vậy bạn nên cẩn thận để không nhầm lẫn chúng

  • Hai tham số của
    const text = "Mozilla";
    console.log(text.substring(2, 5)); // "zil"
    console.log(text.substr(2, 3)); // "zil"
    
    5 là
    const text = "Mozilla";
    console.log(text.substring(2, 5)); // "zil"
    console.log(text.substr(2, 3)); // "zil"
    
    7 và
    const text = "Mozilla";
    console.log(text.substring(2, 5)); // "zil"
    console.log(text.substr(2, 3)); // "zil"
    
    3, trong khi đối với substring(), chúng là
    const text = "Mozilla";
    console.log(text.substring(2, 5)); // "zil"
    console.log(text.substr(2, 3)); // "zil"
    
    7 và
    const text = "Mozilla";
    console.log(text.substring(5, 2)); // "zil"
    console.log(text.slice(5, 2)); // ""
    
    1
  • Chỉ số của
    const text = "Mozilla";
    console.log(text.substring(2, 5)); // "zil"
    console.log(text.substr(2, 3)); // "zil"
    
    7 của
    const text = "Mozilla";
    console.log(text.substring(2, 5)); // "zil"
    console.log(text.substr(2, 3)); // "zil"
    
    5 sẽ quấn đến cuối chuỗi nếu nó âm, trong khi chỉ số của substring() sẽ kẹp nó vào
    const text = "Mozilla";
    
    // Takes 4 last characters of string
    console.log(text.substring(text.length - 4)); // prints "illa"
    
    // Takes 5 last characters of string
    console.log(text.substring(text.length - 5)); // prints "zilla"
    
    4
  • Độ dài âm trong
    const text = "Mozilla";
    console.log(text.substring(2, 5)); // "zil"
    console.log(text.substr(2, 3)); // "zil"
    
    5 được coi là 0, trong khi substring() sẽ hoán đổi hai chỉ số nếu
    const text = "Mozilla";
    console.log(text.substring(5, 2)); // "zil"
    console.log(text.slice(5, 2)); // ""
    
    1 nhỏ hơn
    const text = "Mozilla";
    console.log(text.substring(2, 5)); // "zil"
    console.log(text.substr(2, 3)); // "zil"
    
    7

Hơn nữa,

const text = "Mozilla";
console.log(text.substring(2, 5)); // "zil"
console.log(text.substr(2, 3)); // "zil"
5 được coi là một tính năng kế thừa trong ECMAScript, vì vậy tốt nhất là tránh sử dụng nó nếu có thể

const text = "Mozilla";
console.log(text.substring(2, 5)); // "zil"
console.log(text.substr(2, 3)); // "zil"

Các phương pháp _______ 57 _______ và ________ 35 _______2 hầu như giống hệt nhau, nhưng có một số khác biệt tinh tế giữa hai phương pháp này, đặc biệt là trong cách xử lý các đối số phủ định

Phương thức substring() hoán đổi hai đối số của nó nếu

const anyString = "Mozilla";

console.log(anyString.substring(0, 1)); // 'M'
console.log(anyString.substring(1, 0)); // 'M'

console.log(anyString.substring(0, 6)); // 'Mozill'

console.log(anyString.substring(4)); // 'lla'
console.log(anyString.substring(4, 7)); // 'lla'
console.log(anyString.substring(7, 4)); // 'lla'

console.log(anyString.substring(0, 7)); // 'Mozilla'
console.log(anyString.substring(0, 10)); // 'Mozilla'
1 lớn hơn
const anyString = "Mozilla";

console.log(anyString.substring(0, 1)); // 'M'
console.log(anyString.substring(1, 0)); // 'M'

console.log(anyString.substring(0, 6)); // 'Mozill'

console.log(anyString.substring(4)); // 'lla'
console.log(anyString.substring(4, 7)); // 'lla'
console.log(anyString.substring(7, 4)); // 'lla'

console.log(anyString.substring(0, 7)); // 'Mozilla'
console.log(anyString.substring(0, 10)); // 'Mozilla'
2, nghĩa là một chuỗi vẫn được trả về. Phương thức
console.log(text.substring(-5, 2)); // "Mo"
console.log(text.substring(-5, -2)); // ""
2 trả về một chuỗi rỗng nếu trường hợp này xảy ra

const text = "Mozilla";
console.log(text.substring(5, 2)); // "zil"
console.log(text.slice(5, 2)); // ""

Nếu một trong hai hoặc cả hai đối số là phủ định hoặc

const text = "Mozilla";

// Takes 4 last characters of string
console.log(text.substring(text.length - 4)); // prints "illa"

// Takes 5 last characters of string
console.log(text.substring(text.length - 5)); // prints "zilla"
8, thì phương thức substring() sẽ xử lý chúng như thể chúng là
const text = "Mozilla";

// Takes 4 last characters of string
console.log(text.substring(text.length - 4)); // prints "illa"

// Takes 5 last characters of string
console.log(text.substring(text.length - 5)); // prints "zilla"
4

console.log(text.substring(-5, 2)); // "Mo"
console.log(text.substring(-5, -2)); // ""

console.log(text.substring(-5, 2)); // "Mo"
console.log(text.substring(-5, -2)); // ""
2 cũng coi các đối số của
const text = "Mozilla";

// Takes 4 last characters of string
console.log(text.substring(text.length - 4)); // prints "illa"

// Takes 5 last characters of string
console.log(text.substring(text.length - 5)); // prints "zilla"
8 là
const text = "Mozilla";

// Takes 4 last characters of string
console.log(text.substring(text.length - 4)); // prints "illa"

// Takes 5 last characters of string
console.log(text.substring(text.length - 5)); // prints "zilla"
4, nhưng khi nó được cung cấp các giá trị âm, nó sẽ đếm ngược từ cuối chuỗi để tìm các chỉ mục

console.log(text.slice(-5, 2)); // ""
console.log(text.slice(-5, -2)); // "zil"

Xem trang

console.log(text.substring(-5, 2)); // "Mo"
console.log(text.substring(-5, -2)); // ""
2 để biết thêm ví dụ với số âm

Ví dụ sau thay thế một chuỗi con trong một chuỗi. Nó sẽ thay thế cả ký tự riêng lẻ và chuỗi con. Lệnh gọi hàm ở cuối ví dụ thay đổi chuỗi

console.log(text.slice(-5, 2)); // ""
console.log(text.slice(-5, -2)); // "zil"
4 thành
console.log(text.slice(-5, 2)); // ""
console.log(text.slice(-5, -2)); // "zil"
5

// Replaces oldS with newS in the string fullS
function replaceString(oldS, newS, fullS) {
  for (let i = 0; i < fullS.length; ++i) {
    if (fullS.substring(i, i + oldS.length) === oldS) {
      fullS =
        fullS.substring(0, i) +
        newS +
        fullS.substring(i + oldS.length, fullS.length);
    }
  }
  return fullS;
}

replaceString("World", "Web", "Brave New World");

Lưu ý rằng điều này có thể dẫn đến một vòng lặp vô hạn nếu bản thân

console.log(text.slice(-5, 2)); // ""
console.log(text.slice(-5, -2)); // "zil"
6 là một chuỗi con của
console.log(text.slice(-5, 2)); // ""
console.log(text.slice(-5, -2)); // "zil"
7 — ví dụ: nếu bạn đã cố gắng thay thế '
console.log(text.slice(-5, 2)); // ""
console.log(text.slice(-5, -2)); // "zil"
8' bằng '
console.log(text.slice(-5, 2)); // ""
console.log(text.slice(-5, -2)); // "zil"
9' tại đây

Một phương pháp tốt hơn để thay thế chuỗi như sau

function replaceString(oldS, newS, fullS) {
  return fullS.split(oldS).join(newS);
}

Đoạn mã trên phục vụ như một ví dụ cho hoạt động chuỗi con. Nếu bạn cần thay thế các chuỗi con, hầu hết thời gian bạn sẽ muốn sử dụng

// Replaces oldS with newS in the string fullS
function replaceString(oldS, newS, fullS) {
  for (let i = 0; i < fullS.length; ++i) {
    if (fullS.substring(i, i + oldS.length) === oldS) {
      fullS =
        fullS.substring(0, i) +
        newS +
        fullS.substring(i + oldS.length, fullS.length);
    }
  }
  return fullS;
}

replaceString("World", "Web", "Brave New World");
0

Làm cách nào để có được 3 chữ cái đầu tiên trong JavaScript?

Sử dụng chuỗi. slice() để lấy ba ký tự đầu tiên của một chuỗi , e. g. const first3 = str. lát (0, 3);. Phương thức slice sẽ trả về một chuỗi mới chứa ba ký tự đầu tiên của chuỗi ban đầu.

Làm cách nào để nhận các chữ cái đầu tiên của chuỗi trong JavaScript?

Lấy ký tự đầu tiên của chuỗi . GHI CHÚ. charAt thích hợp hơn là sử dụng [ ] (ký hiệu ngoặc) như str. charAt(0) trả về một chuỗi rỗng ( '' ) cho str = '' thay vì không xác định trong trường hợp ''[0]. use the charAt() method, at index 0, to select the first character of the string. NOTE: charAt is preferable than using [ ] (bracket notation) as str. charAt(0) returns an empty string ( '' ) for str = '' instead of undefined in case of ''[0] .