我正在尝试检查字符串是否包含 C 中的子字符串,例如:
char *sent = "this is my sample example";
char *word = "sample";
if (/* sentence contains word */) {
/* .. */
}
在 C++ 中用什么代替 string::find
?
最佳答案
if (strstr(sent, word) != NULL) {
/* ... */
}
请注意,如果找到单词 word
,strstr
会返回指向 sent
中单词开头的指针。
关于检查子字符串是否存在于 C 中的字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12784766/