Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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++ 在C+中修剪字符串+;_C++_String - Fatal编程技术网

C++ 在C+中修剪字符串+;

C++ 在C+中修剪字符串+;,c++,string,C++,String,我有一些字符串的格式#包括“filename.h”,我需要从所有这些字符串中拉出filename.h部分。例如,包括“apple.h”#包括“mango.h”#包括“草莓.h” getline(Myfile,str1)//str1现在包含一行文本文件,即include“草莓.h” std::cout“substr(10,…”可以说不是这样做的:) 就个人而言,我建议考虑使用“regex”来处理两个引号之间的所有内容,而不管引号的起始位置或结束位置是什么: 可能是这样的 string::size_

我有一些字符串的格式#包括“filename.h”,我需要从所有这些字符串中拉出filename.h部分。例如,包括“apple.h”#包括“mango.h”#包括“草莓.h”

getline(Myfile,str1)//str1现在包含一行文本文件,即include“草莓.h”
std::cout“substr(10,…”可以说不是这样做的:)

就个人而言,我建议考虑使用“regex”来处理两个引号之间的所有内容,而不管引号的起始位置或结束位置是什么:

可能是这样的

string::size_type beg = str1.find_first_of("\""); //find first quotation mark
string::size_type end = str1.find_first_of("\"", beg + 1); //find next quotation mark
string result = str1.substr( beg + 1 , end - (beg + 1) ); //return the portion in between

编辑:修复了错误,添加了最后一个引号,尝试line.length()-1而不是line.length()

我认为您的思路是正确的,但唯一的问题是
substr
调用的第二个参数不太正确
substr
将所需子字符串的起始偏移量和长度作为其参数。在您的例子中,您需要从第10个到第二个到最后一个字符(最后一个字符是
)的所有字符。因此,您需要一个从10开始到
line.size()-(10+1)
(您必须减去10,因为您首先跳过它们,然后再减去一个,以不包括尾部的

尝试:


cout+1进近!这很接近,但是
size\u t
不是
string
的成员,并且您正在包括关闭的
。请参阅以获得解决方法。您的右侧忘记了在beg结尾添加+1。我想的是string::size\u类型。@Deepak B-我完全不同意。硬编码的值是“坏的”“.user814628的建议可能需要三行字——但这是正确的方法。你真的想“找到引号之间的字符串”。不是“位置10和len-11之间的字符串”。IMHO…@paulsm4:我的假设是行的格式是固定的,只有一组引号,前面正好是字符串
#include
。尝试任意字符串需要比仅仅查找开始和结束引号更复杂的东西(可能有多组引号,引号可能是嵌套的,等等)
string::size_type beg = str1.find_first_of("\""); //find first quotation mark
string::size_type end = str1.find_first_of("\"", beg + 1); //find next quotation mark
string result = str1.substr( beg + 1 , end - (beg + 1) ); //return the portion in between
cout<<x.substr(10,line.size()-11)<<endl;