Share xóa khoảng trắng trong chuỗi c++,

#C ++, #String, #Remove Spaces, #Trim, #C ++ Chuỗi ## Xóa không gian trong chuỗi C ++

Các chuỗi C ++ là chuỗi các ký tự và không gian được coi là ký tự.Vì vậy, để xóa khoảng trắng khỏi chuỗi C ++, bạn có thể sử dụng các phương thức sau:

1. ** Sử dụng phương thức `erase ()`. ** Phương thức `erase ()` lấy chỉ mục bắt đầu và chỉ mục kết thúc làm đối số.Chỉ số bắt đầu là vị trí của ký tự đầu tiên bị xóa và chỉ số kết thúc là vị trí của ký tự sau khi ký tự cuối cùng bị xóa.Ví dụ: mã sau sẽ xóa tất cả các khoảng trống khỏi chuỗi `" Hello World! "`:

`` `C ++
std :: String str = "Hello World!";
str.erase (0, str.find (''));
str.erase (str.find_last_of ('') + 1);
std :: cout << str << std :: endl;// In "Helloworld!"
`` `

2. ** Sử dụng phương thức `thay thế ()`Chỉ số bắt đầu và chỉ mục kết thúc giống như đối với phương thức `erase ()`.Chuỗi mới là chuỗi sẽ thay thế các ký tự giữa chỉ mục bắt đầu và chỉ mục kết thúc.Ví dụ: mã sau sẽ xóa tất cả các khoảng trống khỏi chuỗi `" Hello World! "`:

`` `C ++
std :: String str = "Hello World!";
str.replace (0, str.find (''), "");
str.replace (str.find_last_of ('') + 1, str.length (), "");
std :: cout << str << std :: endl;// In "Helloworld!"
`` `

3. ** Sử dụng lớp `std :: String_view`. ** Lớp` std :: String_view` là một lớp nhẹ đại diện cho chế độ xem của một chuỗi.Nó không sở hữu dữ liệu chuỗi, vì vậy nó không cần phải phân bổ bất kỳ bộ nhớ nào.Điều này làm cho nó trở thành một cách hiệu quả hơn để xóa các khoảng trống khỏi một chuỗi hơn là sử dụng lớp `std :: String`.Ví dụ: mã sau sẽ xóa tất cả các khoảng trống khỏi chuỗi `" Hello World! "`:

`` `C ++
std :: String_view str = "Hello World!";
str.remove_spaces ();
std :: cout << str << std :: endl;// In "Helloworld!"
`` `

## hashtags

* #C ++
* #sợi dây
* #Remove không gian
* #Trim
* #C ++ Chuỗi
=======================================
#C++, #String, #Remove spaces, #Trim, #C++String ## Delete spaces in a C++ string

C++ strings are sequences of characters, and spaces are considered characters. So, to delete spaces from a C++ string, you can use the following methods:

1. **Use the `erase()` method.** The `erase()` method takes a starting index and an ending index as arguments. The starting index is the position of the first character to be deleted, and the ending index is the position of the character after the last character to be deleted. For example, the following code deletes all spaces from the string `"Hello world!"`:

```c++
std::string str = "Hello world!";
str.erase(0, str.find(' '));
str.erase(str.find_last_of(' ') + 1);
std::cout << str << std::endl; // prints "Helloworld!"
```

2. **Use the `replace()` method.** The `replace()` method takes a starting index, an ending index, and a new string as arguments. The starting index and ending index are the same as for the `erase()` method. The new string is the string that will replace the characters between the starting index and the ending index. For example, the following code deletes all spaces from the string `"Hello world!"`:

```c++
std::string str = "Hello world!";
str.replace(0, str.find(' '), "");
str.replace(str.find_last_of(' ') + 1, str.length(), "");
std::cout << str << std::endl; // prints "Helloworld!"
```

3. **Use the `std::string_view` class.** The `std::string_view` class is a lightweight class that represents a view of a string. It does not own the string data, so it does not need to allocate any memory. This makes it a more efficient way to delete spaces from a string than using the `std::string` class. For example, the following code deletes all spaces from the string `"Hello world!"`:

```c++
std::string_view str = "Hello world!";
str.remove_spaces();
std::cout << str << std::endl; // prints "Helloworld!"
```

## Hashtags

* #C++
* #String
* #Remove spaces
* #Trim
* #C++String
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top