简体   繁体   English

strstr()在同一行中搜索两个不同的字符串

[英]strstr() to search two different strings in the same line

I am trying to search two different strings in a line using strstr. 我正在尝试使用strstr在一行中搜索两个不同的字符串。

sBuffer = "This is app test"
s1= strstr (sBuffer, "This");
s2= strstr (sBuffer, "test");
printf("%s\n", s1); //prints - This is app test 
printf("%s\n", s2); //prints - test
if (s1 && s2)
    //do something

Expected output for s1 should be the string "This" but it is printing the entire string for s1. s1的预期输出应为字符串"This"但它将打印s1的整个字符串。

s2 however is printed correctly. s2但是正确打印。

Any help appreciated. 任何帮助表示赞赏。

EDIT: Although all the answers are correct (upvoted all answers), I am accepting dasblinkenlight's answer. 编辑:尽管所有答案都是正确的(所有答案均已赞成),但我接受dasblinkenlight的答案。 This is because I realize checking the boolean condition as shown below would suffice my requirement. 这是因为我意识到检查如下所示的布尔条件就可以满足我的要求。 Thanks for all the answers. 感谢所有的答案。

if ( (strstr (sBuffer, "This")) && (strstr (sBuffer, "test")) )
        //do something

You're not understanding what the function does. 您不了解该功能的作用。

It gives you the address in sBuffer ("the haystack") where the search string ("the needle") has been found. 它为您提供了在sBuffer (“大海捞针”)中找到搜索字符串(“ needle”)的地址。 It doesn't modify the haystack string, so it won't terminate the sub-string. 它不会修改干草堆字符串,因此不会终止子字符串。

You have: 你有:

         +---+---+---+---+---+---+---+--+---+---+---+---+---+---+---+---+----+
sBuffer: | T | h | i | s |   | i | s |  | a | p | p |   | t | e | s | t | \0 |
         +---+---+---+---+---+---+---+--+---+---+---+---+---+---+---+---+----+
           ^                                              ^
           |                                              |
           |                                              |
  strstr(sBuffer, "Test")                        strstr(sBuffer, "test")

As you can see, strstr(sBuffer, "Test") will simply return sBuffer , which of course still contains the rest of the characters, it's the same memory buffer. 如您所见, strstr(sBuffer, "Test")将仅返回sBuffer ,当然它仍然包含其余字符,它是相同的内存缓冲区。

If you need to extract the sub-string that you found, you must do so yourself. 如果需要提取找到的子字符串,则必须自己提取。 A suitable function to use is strlcpy() if you have it, else strncpy() will work since you know the exact length of the data to copy. 如果您拥有合适的函数,请使用strlcpy() ,否则strncpy()将起作用,因为您知道要复制的数据的确切长度。

strstr() returns a pointer to the first character of the substring it found. strstr()返回指向找到的子字符串的第一个字符的指针。 It doesn't NUL-terminate the string after the searched substring, this is the expected and correct behavior. 它不会NUL终止搜索的子字符串之后的字符串,这是预期的正确行为。

As to the solution: if you have a non- const string, you can simply modify it so that it's NUL-terminated at the correct position (but then beware of the modifications you made). 关于解决方案:如果您有一个非const字符串,则可以简单地对其进行修改,以使其在正确的位置处被NUL终止(但是请注意所做的修改)。 If not, then make a copy of the substring. 如果不是,则复制该子字符串。

const char *haystack = "abcd efgh ijkl";
const char *needle   = "efgh";

const char *p = strstr(haystack, needle);
if (p) {
    size_t l = strlen(needle);
    char buf[l + 1];
    memcpy(buf, p, l);
    buf[l] = 0;
    printf("%s\n", buf);
}

The return value of strstr is the pointer to the original, unmodified, string at the point of the match. strstr的返回值是指向匹配点处未经修改的原始字符串的指针。 The reason why the second call displays test is a coincidence: test simply happens to be at the end of the searched string. 第二个调用显示test的原因是一个巧合: test只是碰巧在搜索到的字符串的末尾。 Had the sBuffer been "This is app test of strstr" , the output for the second call would be test of strstr , not simply test . 如果sBuffer"This is app test of strstr" ,则第二个调用的输出将是test of strstr ,而不仅仅是test

To fix this, you can change your program like this: 要解决此问题,您可以像这样更改程序:

printf("%s\n", s1 ? "This" : "");
printf("%s\n", s2 ? "test" : "");

The reason this works is that you know that the only case when strstr would return a non-null pointer is when it finds the exact match to what you've been searching for. 之所以起作用,是因为您知道, strstr返回非空指针的唯一情况是找到与您要查找的内容完全匹配的字符串。 If all you need is a boolean "found/not found" flag, you can simply test s1 and s2 for NULL . 如果您只需要一个布尔值“ found / not found”标志,则只需测试s1s2NULL You are using this trick already in your final if statement. 您在最终的if语句中已经在使用此技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM