Cho một chuỗi và tác vụ là xóa một ký tự khỏi chuỗi đã cho.
Phương pháp 1: Sử dụng phương thức thay thế (): Phương thức thay thế được sử dụng để thay thế một ký tự/chuỗi cụ thể bằng ký tự/chuỗi khác. Phải mất hai tham số, đầu tiên là chuỗi được thay thế và thứ hai là chuỗi sẽ được thay thế bằng. Trong trường hợp này, tham số đầu tiên là ký tự sẽ được xóa và tham số thứ hai có thể được đưa ra dưới dạng chuỗi trống. Điều này sẽ loại bỏ ký tự khỏi chuỗi. Phương pháp này loại bỏ sự xuất hiện đầu tiên của chuỗi.replace() method: The replace method is used to replace a specific character/string with other character/string. It takes two parameters, first is the string to be replaced and the second is the string which is to be replaced with. In this case, the first parameter is the character which is to be removed and the second parameter can be given as an empty string. This will remove the character from the string. This method removes the first occurrence of the string.
Syntax:
string.replace('characterToReplace', '');Example:
HTML
<!DOCTYPE html>
<html>
<head>
string.replace(/regExp/g, '');1<string.replace(/regExp/g, '');3>
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');6
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');8
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0string.replace(/regExp/g, '');3>
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0head>
<// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7>
string.replace(/regExp/g, '');1<string.substr(0, position - 1) + string.substr(position, string.length);1 string.substr(0, position - 1) + string.substr(position, string.length);2
string.replace(/regExp/g, '');5string.substr(0, position - 1) + string.substr(position, string.length);4
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0string.substr(0, position - 1) + string.substr(position, string.length);1>
string.replace(/regExp/g, '');1<<!DOCTYPE html>1>
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');6
string.replace(/regExp/g, '');5<!DOCTYPE html>6
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<!DOCTYPE html>1>
string.replace(/regExp/g, '');1<<3<4<3>
string.replace(/regExp/g, '');1<<3>
string.replace(/regExp/g, '');5html2
string.replace(/regExp/g, '');5<html5 html6html5>
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<3>
string.replace(/regExp/g, '');1<>5 >6
string.replace(/regExp/g, '');5>8
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0>5>
string.replace(/regExp/g, '');1<<5 <6
string.replace(/regExp/g, '');5<8
<9head0
<9head2
<9head4
head5head6
string.replace(/regExp/g, '');5head8
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<5>
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7>
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0htmlstring.replace(/regExp/g, '');08
Output:
- Trước khi nhấp vào nút:
- Sau khi nhấp vào nút:
Phương pháp 2: Sử dụng phương thức thay thế () với biểu thức chính quy: Phương thức này được sử dụng để loại bỏ tất cả các lần xuất hiện của ký tự được chỉ định, không giống như phương thức trước đó. Một biểu thức thông thường được sử dụng thay vì chuỗi cùng với thuộc tính toàn cầu. Nó sẽ chọn mọi lần xuất hiện trong chuỗi và nó có thể được xóa. & NBSP; This method is used to remove all occurrences of the specified character, unlike the previous method. A regular expression is used instead of the string along with the global property. It will select every occurrence in the string and it can be removed.
Syntax:
string.replace(/regExp/g, '');Example:
HTML
<!DOCTYPE html>
<html>
<head>
string.replace(/regExp/g, '');1<string.replace(/regExp/g, '');3>
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');6
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');8
string.replace(/regExp/g, '');1<string.substr(0, position - 1) + string.substr(position, string.length);1 string.substr(0, position - 1) + string.substr(position, string.length);2
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0head>
<// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7>
string.replace(/regExp/g, '');1<string.substr(0, position - 1) + string.substr(position, string.length);1 string.substr(0, position - 1) + string.substr(position, string.length);2
string.replace(/regExp/g, '');5string.substr(0, position - 1) + string.substr(position, string.length);4
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0string.substr(0, position - 1) + string.substr(position, string.length);1>
string.replace(/regExp/g, '');1<<!DOCTYPE html>1>
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');6
string.replace(/regExp/g, '');5<!DOCTYPE html>6
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<!DOCTYPE html>1>
string.replace(/regExp/g, '');1<<3<4<3>
string.replace(/regExp/g, '');1<<3>
string.replace(/regExp/g, '');5html2
string.replace(/regExp/g, '');5<html5 html6html5>
string.replace(/regExp/g, '');1<>5 >6
string.replace(/regExp/g, '');1<>5 >6
string.replace(/regExp/g, '');5>8
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0>5>
string.replace(/regExp/g, '');1<<5 <6
string.replace(/regExp/g, '');5<8
<9head0
<9string.replace(/regExp/g, '');97
<9head4
head5head6
string.replace(/regExp/g, '');5head8
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<5>
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7>
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0htmlstring.replace(/regExp/g, '');08
Output:
- Trước khi nhấp vào nút:
- Sau khi nhấp vào nút:
Phương pháp 2: Sử dụng phương thức thay thế () với biểu thức chính quy: Phương thức này được sử dụng để loại bỏ tất cả các lần xuất hiện của ký tự được chỉ định, không giống như phương thức trước đó. Một biểu thức thông thường được sử dụng thay vì chuỗi cùng với thuộc tính toàn cầu. Nó sẽ chọn mọi lần xuất hiện trong chuỗi và nó có thể được xóa. & NBSP; The slice() method is used to extract parts of a string between the given parameters. This method takes the starting index and the ending index of the string and returns the string in between these indices. If the ending index is not specified, it is assumed to be the length of the string. The first character could be removed by specifying the beginning index to be 1. It extracts the string from the second character up to the end of the string. The last character could be removed by specifying the ending index to be one less than the length of the string. This extracts the string from the beginning of the string to the second to last character.
Syntax:
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);Example:
HTML
<!DOCTYPE html>
<html>
<head>
string.replace(/regExp/g, '');1<string.replace(/regExp/g, '');3>
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');6
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');8
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0string.replace(/regExp/g, '');3>
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0head>
<// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7>
string.replace(/regExp/g, '');1<string.substr(0, position - 1) + string.substr(position, string.length);1 string.substr(0, position - 1) + string.substr(position, string.length);2
string.replace(/regExp/g, '');5<html5 html6html5>
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0string.substr(0, position - 1) + string.substr(position, string.length);1>
string.replace(/regExp/g, '');1<<!DOCTYPE html>1>
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');6
string.replace(/regExp/g, '');5<!DOCTYPE html>6
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<!DOCTYPE html>1>
string.replace(/regExp/g, '');1<<3<4<3>
string.replace(/regExp/g, '');1<<3>
string.replace(/regExp/g, '');5// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);72
string.replace(/regExp/g, '');1<>5 >6
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<3>
string.replace(/regExp/g, '');1<<3>
string.replace(/regExp/g, '');5// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);88
string.replace(/regExp/g, '');5<html5 // Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);92html5>
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<3>
string.replace(/regExp/g, '');1<>5 >6
string.replace(/regExp/g, '');5>8
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0>5>
string.replace(/regExp/g, '');1<<5 <6
string.replace(/regExp/g, '');5<8
<9head0
<9string.substr(0, position - 1) + string.substr(position, string.length);18
<9string.substr(0, position - 1) + string.substr(position, string.length);20
string.substr(0, position - 1) + string.substr(position, string.length);21string.substr(0, position - 1) + string.substr(position, string.length);22
<9string.substr(0, position - 1) + string.substr(position, string.length);24
head5string.substr(0, position - 1) + string.substr(position, string.length);26
<9string.substr(0, position - 1) + string.substr(position, string.length);28
head5string.substr(0, position - 1) + string.substr(position, string.length);30
string.replace(/regExp/g, '');5head8
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<5>
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7>
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0htmlstring.replace(/regExp/g, '');08
Output:
- Trước khi nhấp vào nút:
- Sau khi nhấp vào nút:
Phương pháp 4: Loại bỏ một ký tự cụ thể tại chỉ mục đã cho bằng phương thức Subr (): Phương thức này có thể được sử dụng để xóa một ký tự khỏi một chỉ mục cụ thể trong chuỗi. Phương thức chuỗi con () được sử dụng để trích xuất các phần của chuỗi giữa các tham số đã cho. Phương thức này có hai tham số, một là chỉ mục bắt đầu và một là chỉ mục kết thúc của chuỗi. Chuỗi giữa các chỉ số này được trả về. Phần của chuỗi trước và sau khi ký tự được loại bỏ được tách ra và kết hợp với nhau. Điều này loại bỏ ký tự khỏi chỉ số cụ thể. This method can be used to remove a character from a particular index in the string. The substr() method is used to extract parts of a string between the given parameters. This method takes two parameters, one is the starting index and the other is the ending index of the string. The string between these indices is returned. The portion of the string before and after the character to be removed is separated and joined together. This removes the character from the specific index.
Syntax:Syntax:
string.substr(0, position - 1) + string.substr(position, string.length);Example:
HTML
<!DOCTYPE html>
<html>
<head>
string.replace(/regExp/g, '');1<string.replace(/regExp/g, '');3>
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');6
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');8
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0string.replace(/regExp/g, '');3>
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0head>
<// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7>
string.replace(/regExp/g, '');1<string.substr(0, position - 1) + string.substr(position, string.length);1 string.substr(0, position - 1) + string.substr(position, string.length);2
string.replace(/regExp/g, '');5string.substr(0, position - 1) + string.substr(position, string.length);4
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0string.substr(0, position - 1) + string.substr(position, string.length);1>
string.replace(/regExp/g, '');1<<!DOCTYPE html>1>
string.replace(/regExp/g, '');5string.replace(/regExp/g, '');6
string.replace(/regExp/g, '');5<!DOCTYPE html>6
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<!DOCTYPE html>1>
string.replace(/regExp/g, '');1<<3<4<3>
string.replace(/regExp/g, '');1<<3>
string.replace(/regExp/g, '');5html2
string.replace(/regExp/g, '');5<html5 html6html5>
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<3>
string.replace(/regExp/g, '');1<>5 <!DOCTYPE html>15
string.replace(/regExp/g, '');5>8
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0>5>
string.replace(/regExp/g, '');1<<5 <6
Trước khi nhấp vào nút:
<9head0
<9<!DOCTYPE html>31
<9<!DOCTYPE html>33
<9<!DOCTYPE html>35
string.replace(/regExp/g, '');5head8
string.replace(/regExp/g, '');1// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0<5>
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);7>
// Removing the first character string.slice(1); // Removing the last character string.slice(0, string.length - 1);0htmlstring.replace(/regExp/g, '');08
Output:
- Trước khi nhấp vào nút:
- Sau khi nhấp vào nút:
Phương pháp 4: Loại bỏ một ký tự cụ thể tại chỉ mục đã cho bằng phương thức Subr (): Phương thức này có thể được sử dụng để xóa một ký tự khỏi một chỉ mục cụ thể trong chuỗi. Phương thức chuỗi con () được sử dụng để trích xuất các phần của chuỗi giữa các tham số đã cho. Phương thức này có hai tham số, một là chỉ mục bắt đầu và một là chỉ mục kết thúc của chuỗi. Chuỗi giữa các chỉ số này được trả về. Phần của chuỗi trước và sau khi ký tự được loại bỏ được tách ra và kết hợp với nhau. Điều này loại bỏ ký tự khỏi chỉ số cụ thể.