C++ 如何从字符串中找到子字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13195353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:04:24  来源:igfitidea点击:

How to find substring from string?

c++c

提问by CrazyCoder

How do I find a substring from the string path "/user/desktop/abc/post/" using C/C++? I want to check if folder "abc" is present or not in that path.

如何使用 C/C++ 从字符串路径“/user/desktop/abc/post/”中找到子字符串?我想检查该路径中是否存在文件夹“abc”。

Path is character pointer char *ptr = "/user/desktop/abc/post/";

路径是字符指针 char *ptr = "/user/desktop/abc/post/";

采纳答案by unwind

In C, use the strstr()standard library function:

在 C 中,使用strstr()标准库函数:

const char *str = "/user/desktop/abc/post/";
const int exists = strstr(str, "/abc/") != NULL;

Take care to not accidentally find a too-short substring (this is what the starting and ending slashes are for).

注意不要意外找到太短的子字符串(这是开始和结束斜线的用途)。

回答by Luchian Grigore

Use std::stringand find.

使用std::stringfind

std::string str = "/user/desktop/abc/post/";
bool exists = str.find("/abc/") != std::string::npos;

回答by DragonB

Example using std::stringfind method:

使用std::stringfind 方法的示例:

#include <iostream>
#include <string>

int main (){
    std::string str ("There are two needles in this haystack with needles.");
    std::string str2 ("needle");

    size_t found = str.find(str2);
    if(found!=std::string::npos){ 
        std::cout << "first 'needle' found at: " << found << '\n';
    }

    return 0;
}

Result:

结果:

first 'needle' found at: 14.

回答by 1''

As user1511510 has identified, there's an unusual case when abc is at the end of the file name. We need to look for either /abc/or /abcfollowed by a string-terminator '\0'. A naive way to do this would be to check if either /abc/or /abc\0are substrings:

正如 user1511510 所确定的那样,当 abc 位于文件名末尾时,这是一种不寻常的情况。我们需要查找/abc//abc后跟一个字符串终止符'\0'。一种天真的方法是检查/abc//abc\0是否是子字符串:

#include <stdio.h>
#include <string.h>

int main() {
    const char *str = "/user/desktop/abc";
    const int exists = strstr(str, "/abc/") || strstr(str, "/abc
#include <stdio.h>
#include <string.h>

int main() {
    const char *str = "/user/desktop/abc", *substr;
    const int exists = (substr = strstr(str, "/abc")) && (substr[4] == '
char * str_str(const char *s, const char *t)
{
int i, j, k;
for (i = 0; s[i] != '##代码##'; i++) 
{
for (j=i, k=0; t[k]!='##代码##' && s[j]==t[k]; j++, k++);
if (k > 0 && t[k] == '##代码##')
return (&s[i]);
}
return NULL;
}
' || substr[4] == '/'); printf("%d\n",exists); return 0; }
"); printf("%d\n",exists); return 0; }

but existswill be 1 even if abc is not followed by a null-terminator. This is because the string literal "/abc\0"is equivalent to "/abc". A better approach is to test if /abcis a substring, and then see if the character after this substring (indexed using the pointer returned by strstr()) is either a /or a '\0':

exists即使 abc 后面没有空终止符,也会为 1。这是因为字符串文字"/abc\0"等效于"/abc". 更好的方法是测试 if/abc是子字符串,然后查看此子字符串后面的字符(使用 返回的指针索引strstr())是 a/还是 a '\0'

##代码##

This should work in all cases.

这应该适用于所有情况。

回答by Omkant

Use strstr(const char *s , const char *t)and include<string.h>

使用strstr(const char *s , const char *t)include<string.h>

You can write your own function which behaves same as strstrand you can modify according to your requirement also

您可以编写自己的功能,其行为与strstr您的要求相同,也可以根据您的要求进行修改

##代码##

回答by NatiSpark

If you are utilizing arrays too much then you should include cstring.hbecause it has too many functions including finding substrings.

如果您过多地使用数组,那么您应该包括在内,cstring.h因为它具有太多功能,包括查找子字符串。