CString str = "12345"; int pos = str.FindOneOf("4"); 然后截掉最后4后面的数据,拼接字符串6
时间: 2024-03-03 15:52:47 浏览: 13
您可以使用以下代码实现:
```cpp
CString str = "12345";
int pos = str.FindOneOf("4");
if (pos != -1) {
str = str.Left(pos) + "6";
}
cout << str << endl;
```
输出为:`1236`。
代码解释:首先在字符串`str`中查找字符`4`,得到它在字符串中的位置。然后使用`CString::Left()`函数获取从字符串开头到这个位置的子字符串,再加上字符串`6`即可得到最终结果。最后输出结果即可。
相关问题
CString str = “../uem/1.exe”,去掉最后一个/后的数据,接上2.tar.gz,然后输出
您可以使用以下代码来实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "../uem/1.exe";
size_t pos = str.rfind('/');
str = str.substr(0, pos) + "/2.tar.gz";
cout << str << endl;
return 0;
}
```
输出为:`../uem/2.tar.gz`。
代码解释:`string::rfind()`函数可以查找最后一个出现的指定字符('/')的位置。然后使用`string::substr()`函数获取从0到这个位置的子字符串,再加上`/2.tar.gz`即可。最后输出结果即可。
CString str=../conf/adf/df/1.txt ;截取字符串,获取../conf/adf/df
可以使用字符串截取函数 substr() 来实现,具体代码如下:
```
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "../conf/adf/df/1.txt";
string subStr = str.substr(0, str.find_last_of('/'));
cout << subStr << endl;
return 0;
}
```
输出结果为:
```
../conf/adf/df
```
其中,substr() 函数的第一个参数是起始位置,第二个参数是要截取的字符个数。find_last_of() 函数是查找字符在字符串中最后一次出现的位置,本例中是查找最后一个 '/' 的位置。因此,subStr 就是截取出来的子字符串。