例如,验证有效的URL,我想做到以下几点如何检查字符串是否以C中的某个字符串开头?
char usUrl[MAX] = "http://www.stackoverflow"
if(usUrl[0] == 'h'
&& usUrl[1] == 't'
&& usUrl[2] == 't'
&& usUrl[3] == 'p'
&& usUrl[4] == ':'
&& usUrl[5] == '/'
&& usUrl[6] == '/') { // what should be in this something?
printf("The Url starts with http:// \n");
}
或者说,我已经想过使用strcmp(str, str2) == 0
,但是这必须是非常复杂的。
有没有一个标准的C函数来做这样的事情?
请尝试'strncmp'。 – congusbongus 2013-03-20 04:07:32
可能在C中使用类似'\ startsWith(str \ _a,str \ _b)\'的东西的重复](http://stackoverflow.com/questions/4770985/something-like-startswithstr-a-str-b- in-c) – 2015-06-09 20:20:57