Article Directory
- Summary
- text:
-
- QString interception function
-
- (1) left(n): Intercept the first n characters of the string.
- (2) right(n): Intercept the last n characters of the string.
- (3) mid(pos, n): Intercept n characters of the string starting from position pos.
- (4) chopped(n): Remove n characters at the end of the string.
- (5) section(sep, start, end): Use sep as the separator, and intercept the substring from the start position to the end position.
- std::string interception function
-
- (1) substr(pos, n): Intercept n characters of the string starting from position pos.
- (2) erase(pos, n): Delete n characters of the string starting from position pos.
- (3) replace(pos, n, new_str): Replace the n characters of the string from position pos with new_str.
- (4) find(sub_str): Find the first position of the substring sub_str in the string.
- (5) rfind(sub_str): Find the last position of the substring sub_str in the string.
- Summarize:
Summary
Abstract: This article will introduce the related functions of using QString and std::string to intercept in Qt. We will detail various commonly used interception functions and how to use them to help you be more flexible and efficient in string processing.
text:
QString interception function
(1) left(n): Intercept the first n characters of the string.
QString str = "Hello, World!";
QString result = str.left(5); // 结果为"Hello"
(2) right(n): Intercept the last n characters of the string.
QString str = "Hello, World!";
QString result = str.right(6); // 结果为"World!"
(3) mid(pos, n): Intercept n characters of the string starting from position pos.
QString str = "Hello, World!";
QString result = str.mid(7, 5); // 结果为 "World"
(4) chopped(n): Remove n characters at the end of the string.
QString str = "Hello, World!";
QString result = str.chopped(7); // 结果为 "Hello"
(5) section(sep, start, end): Use sep as the separator, and intercept the substring from the start position to the end position.
```cpp
QString str = "apple,orange,banana";
QString result = str.section(',', 1, 1); // 结果为"orange"
std::string interception function
(1) substr(pos, n): Intercept n characters of the string starting from position pos.
std::string str = "Hello, World!";
std::string result = str.substr(7, 5); // 结果为 "World"
(2) erase(pos, n): Delete n characters of the string starting from position pos.
std::string str = "Hello, World!";
str.erase(0, 7); // 结果为"World!"
(3) replace(pos, n, new_str): Replace the n characters of the string from position pos with new_str.
std::string str = "Hello, World!";
str.replace(0, 5, "Hi"); // 结果为 "Hi, World!"
(4) find(sub_str): Find the first position of the substring sub_str in the string.
std::string str = "Hello, World!";
size_t pos = str.find("World"); // 结果为 7
(5) rfind(sub_str): Find the last position of the substring sub_str in the string.
std::string str = "Hello, World!";
size_t pos = str.rfind("o"); // 结果为 7
Summarize:
This article introduces the related functions of using QString and std::string to intercept in Qt. You can use these functions to easily intercept substrings of strings, making string processing more flexible and efficient. I hope the content of this article can help you with string processing in Qt development.