c++ - 如何根据字符拆分字符串
问题描述
我可以从以下行获取子字符串
string filename = C:\Shaders\Test\Model\source\scene.fbx"
filename.substr( 0 , filename.find_last_of('\') );
这将是结果
C:\Shaders\Test\Model\source
现在,如果我想获取从最后一个“\”到字符串末尾的子字符串
scene.fbx
我正在尝试这条线。
filename.substr(filename.find_last_of('/'), filename.size() )
但我要崩溃了。
解决方案
#include <iostream>
using namespace std;
int main() {
string s = "C:\\Shaders\\Test\\Model\\source\\scene.fbx";
cout<<s<<endl<<s.substr(s.find_last_of('\\')+1, s.length())<<endl;
// As @David suggested in comments, you can omit `s.length()`
cout<<s.substr(s.find_last_of('\\')+1);
}
输出
C:\Shaders\Test\Model\source\scene.fbx
scene.fbx
scene.fbx
推荐阅读
- python - 如何运行没有定义参数的函数?
- c# - 如何从 Razor 页面访问视图组件的属性
- html - 如何使用 css 在 html 中设置 flex 内联?
- pandas - Pandas groupby,然后在另一列中找到每组的最大值
- octave - 在 Octave 中最小化具有有限变量的线性方程组
- r - R:为什么,如何避免:read.table 通过删除最后一个字符(冒号)将字符(字符串)转换为数字
- node.js - 在 Twilio IVR 中使用 Google Cloud Speech
- java - 使用spring jdbc查询多个表
- python - 为什么 pip 安装的依赖项不在 pipfile 中?
- python - aiohttp 服务器立即停止