indexing - How do I check the end of a string in C++? -
i have rudimentary program i'm trying implement asks url of .pdf file , downloads , shows through xming. first, want check make sure user put in url 'http://' @ front , 'pdf' or 'pdf' @ end. suppose might typical problem coming python, how check end of string user inputs. using method below (which used python-oriented-brain)
range error: -3
so how actual c++ programmers accomplish task? please , thank you.
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'))
in c++ cant access negative indizies. have manually calculate position of laste element:
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')
i'm assuming file c++ - string, if not have use other way length
you simplify code using build in string-functions:
int s = file.size(); if (s > 10 && file.find("http://") == 0 && file.substr(s-3, 3) == "pdf") //...
or use regex comment suggested (probably nicest way)
Comments
Post a Comment