Cách xóa nhiều ký tự trong chuỗi trong C++

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

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
def
ghi
jkl
abdgj
adetyk

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.  

Python3




The original string is : abbabba
The string after replacement of positions : baabaab
0

The original string is : abbabba
The string after replacement of positions : baabaab
1
The original string is : abbabba
The string after replacement of positions : baabaab
2
The original string is : abbabba
The string after replacement of positions : baabaab
3

 

The original string is : abbabba
The string after replacement of positions : baabaab
4

The original string is : abbabba
The string after replacement of positions : baabaab
5
The original string is : abbabba
The string after replacement of positions : baabaab
6
The original string is : abbabba
The string after replacement of positions : baabaab
0
The original string is : abbabba
The string after replacement of positions : baabaab
1
The original string is : abbabba
The string after replacement of positions : baabaab
2
The original string is : abbabba
The string after replacement of positions : baabaab
3

 

The original string is : abbabba
The string after replacement of positions : baabaab
4

The original string is : abbabba
The string after replacement of positions : baabaab
5

The original string is : abbabba
The string after replacement of positions : baabaab
6
The original string is : abbabba
The string after replacement of positions : baabaab
2
The original string is : abbabba
The string after replacement of positions : baabaab
8
The original string is : abbabba
The string after replacement of positions : baabaab
9
The original string is : 34a7bb86abba032
The string after replacement of positions : abbabba
0
The original string is : 34a7bb86abba032
The string after replacement of positions : abbabba
1
The original string is : 34a7bb86abba032
The string after replacement of positions : abbabba
2
The original string is : 34a7bb86abba032
The string after replacement of positions : abbabba
3
The original string is : 34a7bb86abba032
The string after replacement of positions : abbabba
0
The original string is : abbabba
The string after replacement of positions : baabaab
9
The original string is : 34a7bb86abba032
The string after replacement of positions : abbabba
2
The original string is : 34a7bb86abba032
The string after replacement of positions : abbabba
1
The original string is : 34a7bb86abba032
The string after replacement of positions : abbabba
0
The original string is : 34a7bb86abba032
The string after replacement of positions : abbabba
3
The original string is : abbabba
The string after replacement of positions : baabaab
00

 

The original string is : abbabba
The string after replacement of positions : baabaab
01

The original string is : abbabba
The string after replacement of positions : baabaab
5
The original string is : abbabba
The string after replacement of positions : baabaab
6
The original string is : abbabba
The string after replacement of positions : baabaab
04
The original string is : abbabba
The string after replacement of positions : baabaab
1
The original string is : abbabba
The string after replacement of positions : baabaab
06

đầ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.  

Python3




The original string is : abbabba
The string after replacement of positions : baabaab
07
The original string is : abbabba
The string after replacement of positions : baabaab
08

 

The original string is : abbabba
The string after replacement of positions : baabaab
0

The original string is : abbabba
The string after replacement of positions : baabaab
1
The original string is : abbabba
The string after replacement of positions : baabaab
2
The original string is : abbabba
The string after replacement of positions : baabaab
3

 

The original string is : abbabba
The string after replacement of positions : baabaab
4

The original string is : abbabba
The string after replacement of positions : baabaab
5
The original string is : abbabba
The string after replacement of positions : baabaab
6
The original string is : abbabba
The string after replacement of positions : baabaab
0
The original string is : abbabba
The string after replacement of positions : baabaab
1
The original string is : abbabba
The string after replacement of positions : baabaab
2
The original string is : abbabba
The string after replacement of positions : baabaab
3

 

The original string is : abbabba
The string after replacement of positions : baabaab
20

The original string is : abbabba
The string after replacement of positions : baabaab
5

The original string is : abbabba
The string after replacement of positions : baabaab
6
The original string is : abbabba
The string after replacement of positions : baabaab
2
The original string is : abbabba
The string after replacement of positions : baabaab
24____325
The original string is : 34a7bb86abba032
The string after replacement of positions : abbabba
0
The original string is : abbabba
The string after replacement of positions : baabaab
27
The original string is : abbabba
The string after replacement of positions : baabaab
28

 

The original string is : abbabba
The string after replacement of positions : baabaab
01

The original string is : abbabba
The string after replacement of positions : baabaab
5
The original string is : abbabba
The string after replacement of positions : baabaab
6
The original string is : abbabba
The string after replacement of positions : baabaab
04
The original string is : abbabba
The string after replacement of positions : baabaab
1
The original string is : abbabba
The string after replacement of positions : baabaab
06

đầ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ụng

subn() 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?

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.