How to check if string starts with certain string in C?

后端 未结 5 1574
遥遥无期
遥遥无期 2021-01-04 03:42

For example, to validate the valid Url, I\'d like to do the following

char usUrl[MAX] = \"http://www.stackoverflow\"

if(usUrl[0] == \'h\'
   && usUr         


        
5条回答
  •  遥遥无期
    2021-01-04 04:21

    I would suggest this:

    char *checker = NULL;
    
    checker = strstr(usUrl, "http://");
    if(checker == usUrl)
    {
        //you found the match
    
    }
    

    This would match only when string starts with 'http://' and not something like 'XXXhttp://'

    You can also use strcasestr if that is available on you platform.

提交回复
热议问题