Trong ví dụ trên, chúng ta đã truyền hai đối số cho phương thức
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]0, đối số đầu tiên là biểu thức chính quy
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]1, đối số thứ hai là một chuỗi rỗng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]2. vì vậy nó thay thế tất cả các trường hợp dấu phẩy bằng một chuỗi trống
Một cách khác để xóa dấu phẩy ở đầu chuỗi là sử dụng phương thức
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
6 của chuỗi JavaScriptVí dụ, chúng ta có thể viết
const myOriginalString = ",'first string','more','even more'";
const newString = myOriginalString.replace[/^,/, ''];
console.log[newString]
Chúng tôi gọi
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
6 bằng const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
8 để thay thế dấu phẩy ở đầu bằng một chuỗi trốngVà vì vậy,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]3 vẫn giống như trước đâyPhần kết luận
Chúng ta có thể sử dụng các phương thức chuỗi khác nhau để loại bỏ dấu phẩy ở đầu chuỗi
Thêm nội dung tại PlainEnglish. io. Đăng ký nhận bản tin hàng tuần miễn phí của chúng tôi. Theo dõi chúng tôi trên Twitter và LinkedIn. Kiểm tra Sự bất hòa trong cộng đồng của chúng tôi và tham gia Tập thể tài năng của chúng tôi
Chúng tôi có một câu tục ngữ nổi tiếng, được viết bởi Desiderius Erasmus, có chứa dấu phẩy hoặc dấu phẩy cần phải loại bỏ vì nó không có trong bản gốc. Vì vậy, hãy xem xét năm [5] cách để hoàn thành nhiệm vụ này
💬 câu hỏi. Làm cách nào chúng ta viết mã Python để xóa dấu phẩy/dấu phẩy khỏi Chuỗi?
Chúng ta có thể hoàn thành nhiệm vụ này bằng một trong các tùy chọn sau
- Phương pháp 1. Sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
6 - Phương pháp 2. Sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
7 - Phương pháp 3. Sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
8 vàproverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
9 - Phương pháp 4. Sử dụng
0 vàIn the valley of the blind the one-eyed man is king.
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
9 - Phương pháp 5. Sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
6 vàproverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
9 - Thưởng. Xóa dấu phẩy khỏi tệp văn bản
Sự chuẩn bị
Thêm đoạn mã sau vào đầu mỗi đoạn mã. Đoạn mã này sẽ cho phép mã trong bài viết này chạy không có lỗi
const myOriginalString = ",'first string','more','even more'";
const newString = myOriginalString.replace[/^,/, ''];
console.log[newString]
2Phương pháp 1. Sử dụng thay thế[]
Phương pháp này sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]6 từ thư viện chuỗi tích hợp của Python để xóa dấu phẩy/dấu phẩy và trả về một chuỗi mới với các sửa đổi được thực hiện
ví dụ 1. Xóa tất cả Dấu phẩy khỏi Chuỗi
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
Mã này khai báo một chuỗi chứa một dấu phẩy duy nhất và lưu nó vào
In the valley of the blind the one-eyed man is king.
5Tiếp theo,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]6 được thêm vào chuỗi [
In the valley of the blind the one-eyed man is king.
7] và các đối số được truyền cho chuỗi như sau- Đối số đầu tiên là ký tự cần thay thế/xóa [
8]In the valley of the blind the one-eyed man is king.
- Đối số thứ hai là ký tự để thay thế đối số thứ nhất bằng [
9]. Trong trường hợp này, một chuỗi rỗngIn the valley of the blind the one-eyed man is king.
Cuối cùng,
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
00 được lưu và xuất ra thiết bị đầu cuốiđầu ra
________số 8HOẶC
ví dụ 2. Xóa một dấu phẩy khỏi một chuỗi
Nếu chuỗi của bạn có hai [2] dấu phẩy trở lên thì sao?
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
0Điều này có thể được thực hiện bằng cách chuyển đối số thứ ba cho
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
01 số lần xuất hiện [const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
02] để loại bỏ. Trong trường hợp này, dấu phẩy đầu tiên bị xóa và dấu phẩy khác vẫn còn [const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
03]Cuối cùng,
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
00 được lưu và xuất ra thiết bị đầu cuối💡 Lưu ý. Khi chuyển
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
02 sang proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]6, nó bắt đầu xóa số đã chỉ định của [các] ký tự đã chọn từ bên trái của chuỗi
đầu ra
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
9Hoàn hảo
Phương thức chuỗi Python [Hướng dẫn cơ bản]
Xem video này trên YouTube
Phương pháp 2. Sử dụng Regex
Phương thức này gọi thư viện
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
07, loại bỏ tất cả các lần xuất hiện của ký tự được chỉ định và trả về một chuỗi mới có sửa đổiconst myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
1Mã này khai báo một chuỗi chứa một dấu phẩy duy nhất và lưu nó vào
In the valley of the blind the one-eyed man is king.
5Tiếp theo,
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
09 được gọi với ba [3] đối số- Đối số đầu tiên là ký tự cần thay thế/xóa [
8]In the valley of the blind the one-eyed man is king.
- Đối số thứ hai là ký tự để thay thế đối số thứ nhất bằng [
9]. Trong trường hợp này, một chuỗi rỗngIn the valley of the blind the one-eyed man is king.
- Đối số thứ ba là chuỗi để áp dụng điều này cho [______85]
Cuối cùng,
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
00 được lưu và xuất ra thiết bị đầu cuốiđầu ra
________số 8💡 Lưu ý. Không giống như Phương pháp 1, phương pháp này không cung cấp tùy chọn nhập số lần xác định [số lần xuất hiện] để xóa. Trong trường hợp này, tất cả các lần xuất hiện sẽ bị xóa
5 kỹ năng phải có để trở thành lập trình viên [mà bạn chưa biết]
Xem video này trên YouTube
Phương pháp 3. Sử dụng hiểu danh sách
Phương pháp này sử dụng vòng lặp
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]8 và
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
95 để đánh giá, sau đó tách dòng trên ký tự đã chỉ định [const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
96] và loại bỏ ký tự tương tự. Một const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
97 được trả vềproverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]3
Đoạn mã này khai báo một chuỗi chứa hai [2] dấu phẩy và lưu nó vào
In the valley of the blind the one-eyed man is king.
5Tiếp theo,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9 được gọi và truyền ký tự để tách chuỗi trên [
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
96]Cuối cùng,
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
00 được lưu và xuất ra thiết bị đầu cuối ở định dạng const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
97 chứa ba [3] phần tửGiới thiệu đơn giản về hiểu danh sách trong Python
Xem video này trên YouTube
đầu ra
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9
Phương pháp 4. Sử dụng Lambda và tách[]
Phương pháp này sử dụng một
In the valley of the blind the one-eyed man is king.
0 để lặp qua, const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
14 và proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9 chuỗi trên một ký tự được chỉ định. Sau đó, [các] ký tự được chỉ định sẽ bị xóa và kết quả chuyển đổi thành đối tượng
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
16, sau đó thành đối tượng const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
97proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]5
Đoạn mã này khai báo một chuỗi chứa hai [2] dấu phẩy và lưu nó vào
In the valley of the blind the one-eyed man is king.
5Tiếp theo, một
In the valley of the blind the one-eyed man is king.
0 được sử dụng để const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
14 chuỗi và ________ 49 trên ký tự dấu phẩy [________ 296], tạo ra ba [3] phần tửKhi áp dụng
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
16 cho In the valley of the blind the one-eyed man is king.
0 , một đối tượng tương tự như sau được tạoproverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]0
Cuối cùng, đối tượng được chuyển đổi thành
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
97 và const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
00 được gửi đến thiết bị đầu cuối ở định dạng này có chứa ba [3] phần tửHãy chơi Finxter - Hàm Lambda trong Python
Xem video này trên YouTube
đầu ra
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9
Phương pháp 5. Sử dụng thay thế[] và tách[]
Phương pháp đơn giản này sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]6 và
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9 trên một dòng để xóa tất cả các lần xuất hiện của ký tự đã chỉ định [
In the valley of the blind the one-eyed man is king.
8] và trả về một const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
97proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]2
Đoạn mã này khai báo một chuỗi chứa hai [2] dấu phẩy và lưu nó vào
In the valley of the blind the one-eyed man is king.
5Tiếp theo,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]32 được thêm vào
In the valley of the blind the one-eyed man is king.
5, thao tác này sẽ xóa [các] ký tự đã chỉ định khỏi chuỗi. Sau đó, proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9 tách dòng trên ký tự đã chỉ định [______88]
Một danh sách chứa một [1] phần tử trả về
Cuối cùng, để xuất ra
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
00 mà không có dấu ngoặc xung quanh thì phải áp dụng proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]37 vào cuối
đầu ra
________số 8Thưởng. Xóa dấu phẩy khỏi tệp văn bản
Điều gì xảy ra nếu bạn muốn xóa một ký tự đã chỉ định khỏi tất cả các dòng trong tệp văn bản phẳng?
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]4
Nội dung tục ngữ. txt
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]90
Mã này mở và đọc nội dung của
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]91 từng dòng một
Mỗi dòng được đánh giá và ký tự được chỉ định sẽ bị xóa [
In the valley of the blind the one-eyed man is king.
8]. Điều này được gọi một lần nữa vì chúng tôi cũng muốn xóa ký tự xuống dòng [‘proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]93‘]
Cuối cùng, đầu ra lưu vào
const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
00 và được gửi đến thiết bị đầu cuối ở định dạng const myOriginalString = ",'first string','more','even more'";
const [_, ...rest] = myOriginalString.split[','];
const newString = rest.join[',']
console.log[newString]
97 chứa ba [3] phần tửPython One-Liners - Thủ thuật 2 dòng Read File và Strip[]
Xem video này trên YouTube
đầu ra
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]5
Tóm lược
Năm [5] phương pháp xóa dấu phẩy/dấu phẩy khỏi chuỗi này sẽ cung cấp cho bạn đủ thông tin để chọn phương pháp tốt nhất cho yêu cầu mã hóa của bạn