Share c++ string erase

blackcat999

New member
## C ++ chuỗi xóa

#C ++
#sợi dây
#Tẩy xóa
#tutorial
#Programming

** C ++ String Erase () **

Hàm C ++ `Erase ()` được sử dụng để loại bỏ chuỗi con khỏi chuỗi.Cú pháp của hàm `erase ()` như sau:

`` `C ++
chuỗi.erase (start_index, end_index);
`` `

Trong đó `start_index` là chỉ mục của ký tự đầu tiên được xóa và` end_index` là chỉ mục của ký tự sau khi ký tự cuối cùng được xóa.

Ví dụ: mã sau sẽ loại bỏ "ABC" từ chuỗi "ABCDEFG":

`` `C ++
Chuỗi str = "abcdefg";
str.erase (2, 4);
cout << str << endl;// In "Defg"
`` `

Hàm `erase ()` cũng có thể được sử dụng để loại bỏ một ký tự duy nhất khỏi chuỗi.Để làm điều này, chỉ cần chuyển chỉ số của ký tự để được xóa dưới dạng đối số `start_index`.Ví dụ: mã sau sẽ loại bỏ ký tự 'C' khỏi chuỗi "ABCDEFG":

`` `C ++
Chuỗi str = "abcdefg";
str.erase (2);
cout << str << endl;// In "Abdefg"
`` `

Hàm `erase ()` cũng có thể được sử dụng để xóa tất cả các ký tự khỏi chuỗi.Để làm điều này, chỉ cần chuyển `0` là đối số` start_index` và `chuỗi :: npos` là đối số` end_index`.Ví dụ: mã sau sẽ xóa tất cả các ký tự khỏi chuỗi "ABCDEFG":

`` `C ++
Chuỗi str = "abcdefg";
str.erase (0, chuỗi :: npos);
cout << str << endl;// in ""
`` `

Hàm `erase ()` là một công cụ rất hữu ích để thao tác các chuỗi trong C ++.Nó có thể được sử dụng để loại bỏ các chuỗi con không mong muốn, để loại bỏ các ký tự đơn và để xóa tất cả các ký tự khỏi một chuỗi.
=======================================
## C++ String Erase

#C++
#String
#erase
#tutorial
#Programming

**C++ String Erase()**

The C++ `erase()` function is used to remove a substring from a string. The syntax of the `erase()` function is as follows:

```c++
string.erase(start_index, end_index);
```

where `start_index` is the index of the first character to be removed, and `end_index` is the index of the character after the last character to be removed.

For example, the following code removes the substring "abc" from the string "abcdefg":

```c++
string str = "abcdefg";
str.erase(2, 4);
cout << str << endl; // Prints "defg"
```

The `erase()` function can also be used to remove a single character from a string. To do this, simply pass the index of the character to be removed as the `start_index` argument. For example, the following code removes the character 'c' from the string "abcdefg":

```c++
string str = "abcdefg";
str.erase(2);
cout << str << endl; // Prints "abdefg"
```

The `erase()` function can also be used to remove all characters from a string. To do this, simply pass `0` as the `start_index` argument and `string::npos` as the `end_index` argument. For example, the following code removes all characters from the string "abcdefg":

```c++
string str = "abcdefg";
str.erase(0, string::npos);
cout << str << endl; // Prints ""
```

The `erase()` function is a very useful tool for manipulating strings in C++. It can be used to remove unwanted substrings, to remove single characters, and to remove all characters from a string.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top