Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何搜索和打印字符串中的特定部分? 我主要是从C++库中寻找一个标准函数,它将帮助我在字符串中搜索字符,然后从找到的字符中打印出其余的字符串。我有以下情况: #include <string> using std::string; int main() { string myFilePath = "SampleFolder/SampleFile"; // 1. Search inside the string for the '/' character. // 2. Then print everything after that character till the end of the string. // The Objective is: Print the file name. (i.e. SampleFile). return 0; } #包括 使用std::string; int main() { 字符串myFilePath=“SampleFolder/SampleFile”; //1.在字符串中搜索“/”字符。 //2.然后打印该字符后面的所有内容,直到字符串结束。 //目标是:打印文件名(即SampleFile)。 返回0; }_C++_String_Search_Offset_C++ Standard Library - Fatal编程技术网

如何搜索和打印字符串中的特定部分? 我主要是从C++库中寻找一个标准函数,它将帮助我在字符串中搜索字符,然后从找到的字符中打印出其余的字符串。我有以下情况: #include <string> using std::string; int main() { string myFilePath = "SampleFolder/SampleFile"; // 1. Search inside the string for the '/' character. // 2. Then print everything after that character till the end of the string. // The Objective is: Print the file name. (i.e. SampleFile). return 0; } #包括 使用std::string; int main() { 字符串myFilePath=“SampleFolder/SampleFile”; //1.在字符串中搜索“/”字符。 //2.然后打印该字符后面的所有内容,直到字符串结束。 //目标是:打印文件名(即SampleFile)。 返回0; }

如何搜索和打印字符串中的特定部分? 我主要是从C++库中寻找一个标准函数,它将帮助我在字符串中搜索字符,然后从找到的字符中打印出其余的字符串。我有以下情况: #include <string> using std::string; int main() { string myFilePath = "SampleFolder/SampleFile"; // 1. Search inside the string for the '/' character. // 2. Then print everything after that character till the end of the string. // The Objective is: Print the file name. (i.e. SampleFile). return 0; } #包括 使用std::string; int main() { 字符串myFilePath=“SampleFolder/SampleFile”; //1.在字符串中搜索“/”字符。 //2.然后打印该字符后面的所有内容,直到字符串结束。 //目标是:打印文件名(即SampleFile)。 返回0; },c++,string,search,offset,c++-standard-library,C++,String,Search,Offset,C++ Standard Library,提前感谢你的帮助。如果您能帮助我完成代码,我将不胜感激。您可以从最后一个/开始的字符串中提取一个子字符串,但为了最有效(即避免对要打印的数据进行不必要的复制),您可以使用字符串::rfind以及ostream::write: string myFilePath = "SampleFolder/SampleFile"; size_t slashpos = myFilePath.rfind('/'); if (slashpos != string::npos) // make sure we f

提前感谢你的帮助。如果您能帮助我完成代码,我将不胜感激。

您可以从最后一个
/
开始的字符串中提取一个子字符串,但为了最有效(即避免对要打印的数据进行不必要的复制),您可以使用
字符串::rfind
以及
ostream::write

string myFilePath = "SampleFolder/SampleFile";

size_t slashpos = myFilePath.rfind('/');

if (slashpos != string::npos) // make sure we found a '/'
    cout.write(myFilePath.data() + slashpos + 1, myFilePath.length() - slashpos);
else
    cout << myFilePath;
string myFilePath=“SampleFolder/SampleFile”;
size_t slashpos=myFilePath.rfind('/');
if(slashpos!=string::npos)//确保我们找到了一个“/”
cout.write(myFilePath.data()+slashpos+1,myFilePath.length()-slashpos);
其他的

cout您可以从最后一个
/
开始的字符串中提取一个子字符串,但为了最有效(即避免对要打印的数据进行不必要的复制),您可以使用
string::rfind
以及
ostream::write

string myFilePath = "SampleFolder/SampleFile";

size_t slashpos = myFilePath.rfind('/');

if (slashpos != string::npos) // make sure we found a '/'
    cout.write(myFilePath.data() + slashpos + 1, myFilePath.length() - slashpos);
else
    cout << myFilePath;
string myFilePath=“SampleFolder/SampleFile”;
size_t slashpos=myFilePath.rfind('/');
if(slashpos!=string::npos)//确保我们找到了一个“/”
cout.write(myFilePath.data()+slashpos+1,myFilePath.length()-slashpos);
其他的
cout
std::cout
std::coutTry

size\u t pos=myFilePath.rfind('/');
字符串文件名=myFilePath.substr(pos);
试试看

size\u t pos=myFilePath.rfind('/');
字符串文件名=myFilePath.substr(pos);
cout您可以使用_splitpath()参见表格MSDN

您可以使用此STD RTL函数将路径拆分为组件

您可以使用_splitpath()参见表单MSDN


您可以使用此STD RTL函数将路径拆分为组件

根据描述问题目标的这一行:

// The Objective is: Print the file name. (i.e. SampleFile).
您可以使用std::filesystem很好地完成这项工作:

#包括
命名空间fs=std::实验::文件系统;
路径myFilePath(“SampleFolder/SampleFile”);
fs::path filename=myFilePath.filename();
如果只需要文件名而不需要扩展名:

#include <filesystem>
namespace fs = std::experimental::filesystem;

myFilePath("SampleFolder/SampleFile");
fs::path filename = myFilePath.stem();
#包括
命名空间fs=std::实验::文件系统;
myFilePath(“SampleFolder/SampleFile”);
fs::path filename=myFilePath.stem();

基于描述问题目标的这一行:

// The Objective is: Print the file name. (i.e. SampleFile).
您可以使用std::filesystem很好地完成这项工作:

#包括
命名空间fs=std::实验::文件系统;
路径myFilePath(“SampleFolder/SampleFile”);
fs::path filename=myFilePath.filename();
如果只需要文件名而不需要扩展名:

#include <filesystem>
namespace fs = std::experimental::filesystem;

myFilePath("SampleFolder/SampleFile");
fs::path filename = myFilePath.stem();
#包括
命名空间fs=std::实验::文件系统;
myFilePath(“SampleFolder/SampleFile”);
fs::path filename=myFilePath.stem();

检查
npos
会有意义。检查
npos
会有意义。这是第一个斜杠,而不是最后一个。你还必须检查是否找到了斜杠。这是第一个斜杠,不是最后一个。你还必须检查是否找到了斜杠。斜杠的大小可能应该是90%。理想情况下,它是
std::string::size_type
;-)嗯,是的:)但在标准库的每个实现中,size\u type不是都定义为size\t吗?显然,仅仅因为定义了它,它就可以在未来的实现中更改为任何内容。。。甜C++…POS应该是sisitht90%。理想情况下,它是
std::string::size_type
;-)嗯,是的:)但在标准库的每个实现中,size\u type不是都定义为size\t吗?显然,仅仅因为定义了它,它就可以在未来的实现中更改为任何内容。。。甜蜜的C++…