Phương thức
let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
0 là một phương thức tích hợp sẵn của đối tượng loại let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
1 cho phép bạn tìm kiếm chuỗi của mình cho một giá trị chuỗi đã chỉ định và thay thế nó bằng một giá trị chuỗi mớiCú pháp như hình bên dưới
String.replace[searchString, replaceString];
Phương thức chấp nhận hai tham số
2 cần tìm trong chuỗi. Bạn có thể chuyển một chuỗi chính quy hoặc một biểu thức chính quylet str = "Blue Earth"; let newStr = str.replace["Blue", ""]; console.log[newStr]; // " Earth"
3 để thay thế chuỗi tìm thấylet str = "Blue Earth"; let newStr = str.replace["Blue", ""]; console.log[newStr]; // " Earth"
Bạn có thể sử dụng phương thức
let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
0 để xóa một phần chuỗi của mình bằng cách chuyển một chuỗi trống [let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
5] làm đối số thứ hai của nóVí dụ: mã bên dưới xóa từ
let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
6 khỏi chuỗi let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
7let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
Lưu ý rằng khoảng trống phía trước chữ cái
let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
8 không bị xóa khỏi biến let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
9. Bạn cần thêm khoảng trắng trong đối số đầu tiên khi bạn cũng muốn xóa nóPhương thức
let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
0 không sửa đổi chuỗi ban đầu, vì vậy bạn cần lưu trữ chuỗi mà phương thức trả về trong một chuỗi khácPhương thức này chỉ thay thế lần xuất hiện đầu tiên của tham số
let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
2let str = "Blue Earth Blue";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth Blue"
Nếu muốn loại bỏ tất cả các lần xuất hiện của tham số
let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
2, bạn cần sử dụng phương thức let str = "Blue Earth Blue";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth Blue"
3Phương thức
let str = "Blue Earth Blue";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth Blue"
3 có cùng cú pháp với phương thức let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
0let str = "Blue Earth Blue";
let newStr = str.replaceAll["Blue", ""];
console.log[newStr]; // " Earth "
// further remove the spaces
let nospaceStr = newStr.replaceAll[" ", ""];
console.log[nospaceStr]; // "Earth"
Và đó là cách bạn có thể xóa một phần của chuỗi khỏi chuỗi bằng phương thức
let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
0Xóa một phần của chuỗi bằng String. phương thức chuỗi con[]
Phương thức
let str = "Blue Earth Blue";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth Blue"
7 là một phương thức tích hợp sẵn của đối tượng let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
1 cho phép bạn trích xuất một chuỗi cụ thể từ một chuỗi dài hơnPhương thức này sử dụng chỉ mục của các ký tự trong chuỗi của bạn để xác định nơi bắt đầu và kết thúc quá trình trích xuất
Cú pháp phương thức
let str = "Blue Earth Blue";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth Blue"
7 như hình bên dướiString.substring[startIndex, endIndex];
Phương thức chấp nhận hai tham số
0 để bắt đầu trích xuất chuỗilet str = "Blue Earth Blue"; let newStr = str.replaceAll["Blue", ""]; console.log[newStr]; // " Earth " // further remove the spaces let nospaceStr = newStr.replaceAll[" ", ""]; console.log[nospaceStr]; // "Earth"
1 để kết thúc quá trình trích xuất chuỗi. Chỉ mục bạn vượt qua ở đây được loại trừ khỏi kết quả cuối cùnglet str = "Blue Earth Blue"; let newStr = str.replaceAll["Blue", ""]; console.log[newStr]; // " Earth " // further remove the spaces let nospaceStr = newStr.replaceAll[" ", ""]; console.log[nospaceStr]; // "Earth"
Khi bạn bỏ qua tham số
let str = "Blue Earth Blue";
let newStr = str.replaceAll["Blue", ""];
console.log[newStr]; // " Earth "
// further remove the spaces
let nospaceStr = newStr.replaceAll[" ", ""];
console.log[nospaceStr]; // "Earth"
1 thì chuỗi sẽ được trích ra đầu chuỗi dài hơnChuỗi JavaScript có chỉ số số cho mỗi ký tự trong chuỗi. Nó bắt đầu từ trái sang phải và bắt đầu từ số không
Ví dụ: đây là cách xóa chuỗi
let str = "Blue Earth Blue";
let newStr = str.replaceAll["Blue", ""];
console.log[newStr]; // " Earth "
// further remove the spaces
let nospaceStr = newStr.replaceAll[" ", ""];
console.log[nospaceStr]; // "Earth"
3 khỏi chuỗi let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
7let str = "Blue Earth";
let newStr = str.substring[0,4];
console.log[newStr]; // "Blue"
Và đây là cách xóa chuỗi
let str = "Blue Earth";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth"
6 khỏi cùng một chuỗilet str = "Blue Earth";
let newStr = str.substring[5];
console.log[newStr]; // "Earth"
Sử dụng phương thức
let str = "Blue Earth Blue";
let newStr = str.replace["Blue", ""];
console.log[newStr]; // " Earth Blue"
7, bạn cần chỉ định chỉ số của chuỗi mà bạn muốn giữ lại thay vì chuỗi mà bạn muốn xóa