C++ 如何在C++;?

C++ 如何在C++;?,c++,indexing,C++,Indexing,我有一个我正在尝试实现的基本程序,它要求一个.pdf文件的URL,然后下载它并通过Xming显示出来。首先,我想检查以确保用户确实输入了一个URL,前面是“http://”,后面是“pdf”或“pdf”。我想这可能是来自Python的人的一个典型问题,但我如何检查用户输入的字符串的结尾。使用下面的方法(我在面向Python的大脑中使用过),我得到了一个 那么,实际的C++程序员如何完成这项任务呢?谢谢你 if (file[0]=='h' && file[1]=='t' &

我有一个我正在尝试实现的基本程序,它要求一个.pdf文件的URL,然后下载它并通过Xming显示出来。首先,我想检查以确保用户确实输入了一个URL,前面是“http://”,后面是“pdf”或“pdf”。我想这可能是来自Python的人的一个典型问题,但我如何检查用户输入的字符串的结尾。使用下面的方法(我在面向Python的大脑中使用过),我得到了一个

那么,实际的C++程序员如何完成这项任务呢?谢谢你

if (file[0]=='h' && file[1]=='t' && file[2]=='t' && file[3]=='p' && file[4]==':'
&& file[5]=='/' && file[6]=='/' && (file[-3]=='p' || file[-3]=='P')
&& (file[-2]=='d' || file[-2]=='D') && (file[-1]=='f' || file[-1]=='F'))

在C++中,你不能访问消极的印第安人。 您必须手动计算Last元素的位置:

int s = file.size();
(file[s-3]=='p' || file[s-3]=='P')
&& (file[s-2]=='d' || file[s-2]=='D') 
&& (file[s-1]=='f' || file[s-1]=='F')
我假设文件是C++字符串,如果不是,你必须使用其他方式来获得长度

您还可以通过使用内置字符串函数简化代码:

int s = file.size();
if (s > 10 && file.find("http://") == 0 && file.substr(s-3, 3) == "PDF") //...

或者只使用正则表达式,如另一个注释建议(可能是最好的方法)

< P> C++,你不能访问负的印第安人。 您必须手动计算Last元素的位置:

int s = file.size();
(file[s-3]=='p' || file[s-3]=='P')
&& (file[s-2]=='d' || file[s-2]=='D') 
&& (file[s-1]=='f' || file[s-1]=='F')
我假设文件是C++字符串,如果不是,你必须使用其他方式来获得长度

您还可以通过使用内置字符串函数简化代码:

int s = file.size();
if (s > 10 && file.find("http://") == 0 && file.substr(s-3, 3) == "PDF") //...

或者只使用正则表达式,如另一个注释建议(可能是最好的方法)

< P> C++,你不能访问负的印第安人。 您必须手动计算Last元素的位置:

int s = file.size();
(file[s-3]=='p' || file[s-3]=='P')
&& (file[s-2]=='d' || file[s-2]=='D') 
&& (file[s-1]=='f' || file[s-1]=='F')
我假设文件是C++字符串,如果不是,你必须使用其他方式来获得长度

您还可以通过使用内置字符串函数简化代码:

int s = file.size();
if (s > 10 && file.find("http://") == 0 && file.substr(s-3, 3) == "PDF") //...

或者只使用正则表达式,如另一个注释建议(可能是最好的方法)

< P> C++,你不能访问负的印第安人。 您必须手动计算Last元素的位置:

int s = file.size();
(file[s-3]=='p' || file[s-3]=='P')
&& (file[s-2]=='d' || file[s-2]=='D') 
&& (file[s-1]=='f' || file[s-1]=='F')
我假设文件是C++字符串,如果不是,你必须使用其他方式来获得长度

您还可以通过使用内置字符串函数简化代码:

int s = file.size();
if (s > 10 && file.find("http://") == 0 && file.substr(s-3, 3) == "PDF") //...

或者像另一条评论建议的那样使用Regex(可能是最好的方法)

另一个解决方案是使用


另一个解决方案是使用


另一个解决方案是使用


另一个解决方案是使用

或者您可以使用正则表达式:

#import <regex>
using namespace std;

...

std::string str = "http://www.example.com/myFile.PDF";

std::regex rx("http(s)?:(www\.)?.+/[pP][dD][fF]");

return regex_match(str.begin(), str.end(), rx)

..
#导入
使用名称空间std;
...
std::string str=”http://www.example.com/myFile.PDF";
标准:正则表达式rx(“http(s):(www\)?。+/[pP][dD][fF]”;
返回正则表达式匹配(str.begin()、str.end()、rx)
..
其中:

  • “http”
    -匹配http或https

  • (www\)?
    -匹配www的单个或0个幻影。例如“www.example.com”或“example.com”

  • +/
    -匹配任何字符

  • /[pP][dD][fF]
    -url的结尾可以是构成“pdf”一词的大小写字母的任意组合

您可以查看更多信息,也可以使用正则表达式:

#import <regex>
using namespace std;

...

std::string str = "http://www.example.com/myFile.PDF";

std::regex rx("http(s)?:(www\.)?.+/[pP][dD][fF]");

return regex_match(str.begin(), str.end(), rx)

..
#导入
使用名称空间std;
...
std::string str=”http://www.example.com/myFile.PDF";
标准:正则表达式rx(“http(s):(www\)?。+/[pP][dD][fF]”;
返回正则表达式匹配(str.begin()、str.end()、rx)
..
其中:

  • “http”
    -匹配http或https

  • (www\)?
    -匹配www的单个或0个幻影。例如“www.example.com”或“example.com”

  • +/
    -匹配任何字符

  • /[pP][dD][fF]
    -url的结尾可以是构成“pdf”一词的大小写字母的任意组合

您可以查看更多信息,也可以使用正则表达式:

#import <regex>
using namespace std;

...

std::string str = "http://www.example.com/myFile.PDF";

std::regex rx("http(s)?:(www\.)?.+/[pP][dD][fF]");

return regex_match(str.begin(), str.end(), rx)

..
#导入
使用名称空间std;
...
std::string str=”http://www.example.com/myFile.PDF";
标准:正则表达式rx(“http(s):(www\)?。+/[pP][dD][fF]”;
返回正则表达式匹配(str.begin()、str.end()、rx)
..
其中:

  • “http”
    -匹配http或https

  • (www\)?
    -匹配www的单个或0个幻影。例如“www.example.com”或“example.com”

  • +/
    -匹配任何字符

  • /[pP][dD][fF]
    -url的结尾可以是构成“pdf”一词的大小写字母的任意组合

您可以查看更多信息,也可以使用正则表达式:

#import <regex>
using namespace std;

...

std::string str = "http://www.example.com/myFile.PDF";

std::regex rx("http(s)?:(www\.)?.+/[pP][dD][fF]");

return regex_match(str.begin(), str.end(), rx)

..
#导入
使用名称空间std;
...
std::string str=”http://www.example.com/myFile.PDF";
标准:正则表达式rx(“http(s):(www\)?。+/[pP][dD][fF]”;
返回正则表达式匹配(str.begin()、str.end()、rx)
..
其中:

  • “http”
    -匹配http或https

  • (www\)?
    -匹配www的单个或0个幻影。例如“www.example.com”或“example.com”

  • +/
    -匹配任何字符

  • /[pP][dD][fF]
    -url的结尾可以是构成“pdf”一词的大小写字母的任意组合


您可以查看更多信息,

使用各种字符串方法有许多不同的方法。如果你真的关心性能,你可以用不同的方法进行基准测试。下面是一个使用find&substr的示例

#include <algorithm>
#include <string> 

std::string file = "http://something.pdf";
std::transform(file.begin(), file.end(), file.begin(), ::tolower);  // lowercase the data, see http://stackoverflow.com/questions/313970/stl-string-to-lower-case

if (file.find("http://") == 0 && (file.substr(file.length() - 3) == "pdf")) {
  // valid
}
#包括
#包括
std::字符串文件=”http://something.pdf";
std::transform(file.begin(),file.end(),file.begin(),::tolower);//将数据小写,请参见http://stackoverflow.com/questions/313970/stl-string-to-lower-case
if(file.find(“http:/”==0&&(file.substr(file.length()-3)==pdf”)){
//有效的
}

使用各种字符串方法有很多不同的方法。如果你真的关心性能,你可以用不同的方法进行基准测试。下面是一个使用find&substr的示例

#include <algorithm>
#include <string> 

std::string file = "http://something.pdf";
std::transform(file.begin(), file.end(), file.begin(), ::tolower);  // lowercase the data, see http://stackoverflow.com/questions/313970/stl-string-to-lower-case

if (file.find("http://") == 0 && (file.substr(file.length() - 3) == "pdf")) {
  // valid
}
#包括
#包括
std::字符串文件=”http://something.pdf";
std::transform(file.begin(),file.end(),file.begin(),::tolower);//将数据小写,请参见http://stackoverflow.com/questions/313970/stl-string-to-lower-case
if(file.find(“http:/”)==0&&(file.substr(file.length)()