Có thể xóa chuỗi văn bản khỏi các ô khác nếu chúng lặp lại trong ô trước đó không? . Tôi đang cố giữ lại lần xuất hiện đầu tiên của một chuỗi nhưng xóa chuỗi đó khỏi các ô khác Show
Ví dụ: tôi muốn xóa bất kỳ chuỗi nào lặp lại và dài hơn 2 ký tự abc Vì "ab" nằm ở ô đầu tiên và cũng là ô thứ 5 nên có thể xóa "ab" ở ô thứ 5 được không? "de" cũng lặp lại ở ô thứ 2 và thứ 6, "de" chỉ còn lại ở ô thứ 2 nhưng bị xóa ở ô thứ 6. Ô thứ 2 sẽ vẫn là "def" nhưng ô thứ 6 sẽ chuyển thành "atyk" Việc thay thế ký tự này bằng ký tự khác là một vấn đề phổ biến mà mọi lập trình viên Python trước đây đều phải làm việc. Nhưng đôi khi, chúng tôi yêu cầu giải pháp một dòng đơn giản có thể thực hiện tác vụ cụ thể này. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện. Phương pháp 1. Thay thế nhiều ký tự bằng cách sử dụng thay thế lồng nhau ()Vấn đề này có thể được giải quyết bằng phương pháp thay thế lồng nhau, bên trong sẽ tạo ra một. biến để giữ trạng thái thay thế trung gian. Python3The original string is : abbabba The string after replacement of positions : baabaab0 The original string is : abbabba The string after replacement of positions : baabaab1 The original string is : abbabba The string after replacement of positions : baabaab2 The original string is : abbabba The string after replacement of positions : baabaab3
The original string is : abbabba The string after replacement of positions : baabaab4 The original string is : abbabba The string after replacement of positions : baabaab5 The original string is : abbabba The string after replacement of positions : baabaab6 The original string is : abbabba The string after replacement of positions : baabaab0 The original string is : abbabba The string after replacement of positions : baabaab1 The original string is : abbabba The string after replacement of positions : baabaab2 The original string is : abbabba The string after replacement of positions : baabaab3
The original string is : abbabba The string after replacement of positions : baabaab4 The original string is : abbabba The string after replacement of positions : baabaab5 The original string is : abbabba The string after replacement of positions : baabaab6 The original string is : abbabba The string after replacement of positions : baabaab2 The original string is : abbabba The string after replacement of positions : baabaab8 The original string is : abbabba The string after replacement of positions : baabaab9 The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba0 The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba1 The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba2 The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba3 The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba0 The original string is : abbabba The string after replacement of positions : baabaab9 The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba2 The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba1 The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba0 The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba3 The original string is : abbabba The string after replacement of positions : baabaab00
The original string is : abbabba The string after replacement of positions : baabaab01 The original string is : abbabba The string after replacement of positions : baabaab5 The original string is : abbabba The string after replacement of positions : baabaab6 The original string is : abbabba The string after replacement of positions : baabaab04 The original string is : abbabba The string after replacement of positions : baabaab1 The original string is : abbabba The string after replacement of positions : baabaab06 đầu ra The original string is : abbabba The string after replacement of positions : baabaab Phương pháp 2. Thay thế nhiều ký tự bằng translate() + maketrans()Ngoài ra còn có một chức năng cống hiến có thể thực hiện loại nhiệm vụ thay thế này trong một dòng duy nhất, do đó đây là cách được đề xuất để giải quyết vấn đề cụ thể này. Chỉ hoạt động trong Python2. Python3The original string is : abbabba The string after replacement of positions : baabaab07 The original string is : abbabba The string after replacement of positions : baabaab08
The original string is : abbabba The string after replacement of positions : baabaab0 The original string is : abbabba The string after replacement of positions : baabaab1 The original string is : abbabba The string after replacement of positions : baabaab2 The original string is : abbabba The string after replacement of positions : baabaab3
The original string is : abbabba The string after replacement of positions : baabaab4 The original string is : abbabba The string after replacement of positions : baabaab5 The original string is : abbabba The string after replacement of positions : baabaab6 The original string is : abbabba The string after replacement of positions : baabaab0 The original string is : abbabba The string after replacement of positions : baabaab1 The original string is : abbabba The string after replacement of positions : baabaab2 The original string is : abbabba The string after replacement of positions : baabaab3
The original string is : abbabba The string after replacement of positions : baabaab20 The original string is : abbabba The string after replacement of positions : baabaab5 The original string is : abbabba The string after replacement of positions : baabaab6 The original string is : abbabba The string after replacement of positions : baabaab2 The original string is : abbabba The string after replacement of positions : baabaab24____325 The original string is : 34a7bb86abba032 The string after replacement of positions : abbabba0 The original string is : abbabba The string after replacement of positions : baabaab27 The original string is : abbabba The string after replacement of positions : baabaab28
The original string is : abbabba The string after replacement of positions : baabaab01 The original string is : abbabba The string after replacement of positions : baabaab5 The original string is : abbabba The string after replacement of positions : baabaab6 The original string is : abbabba The string after replacement of positions : baabaab04 The original string is : abbabba The string after replacement of positions : baabaab1 The original string is : abbabba The string after replacement of positions : baabaab06 đầu ra The original string is : abbabba The string after replacement of positions : baabaab Phương pháp 3. Thay thế nhiều ký tự bằng cách sử dụngsubn() tương tự như sub() về mọi mặt, ngoại trừ cách cung cấp đầu ra. Nó trả về một bộ với tổng số thay thế và chuỗi mới thay vì chỉ chuỗi. Làm cách nào để thay thế nhiều ký tự trong một chuỗi trong C?Chúng ta có thể thay thế nhiều ký tự trong một chuỗi bằng cách sử dụng replace() , regex. vòng lặp sub(), translate() hoặc for trong python. Nó thay thế tất cả các lần xuất hiện của Ký tự 's' bằng 'X'. Ký tự 'a' với 'Y'
Làm cách nào để xóa một ký tự khỏi chuỗi C? . s = (char*)str; . . Thiếu newstr[j] = '\0'; Làm cách nào để xóa tất cả các ký tự khỏi chuỗi sau một ký tự cụ thể?Để xóa mọi thứ sau một ký tự cụ thể trong một chuỗi. Sử dụng Chuỗi. phương thức split() để tách chuỗi trên ký tự .
Làm cách nào để xóa hai ký tự khỏi chuỗi trong C#?Việc xóa các ký tự trong C# có thể được thực hiện thông qua phương thức Remove() . Nó xóa các ký tự trong một chuỗi và trả về một chuỗi mới không có các ký tự đã xóa. Nếu độ dài không được chỉ định, nó sẽ xóa tất cả các ký tự từ chỉ mục đầu cho đến cuối. |