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

时间:2021-10-05 19:11:35

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/";

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

6 个解决方案

#1


17  

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).

注意不要偶然地发现一个太短的子字符串(这是开始和结束斜线的目的)。

#2


35  

Use std::string and find.

使用std::string和发现。

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

#3


8  

Example using std::string find method:

示例使用std::string find方法:

#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.

#4


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 /abc followed by a string-terminator '\0'. A naive way to do this would be to check if either /abc/ or /abc\0 are 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\0");
    printf("%d\n",exists);
    return 0;
}

but exists will 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 /abc is a substring, and then see if the character after this substring (indexed using the pointer returned by strstr()) is either a / or a '\0':

但是,即使abc没有被一个空终止符跟踪,它仍然是1。这是因为字符串文字“/abc\0”等同于“/abc”。更好的方法是测试/abc是否是子字符串,然后查看这个子字符串(使用strstr()返回的指针进行索引)后面的字符是A /还是'\0':

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

int main() {
    const char *str = "/user/desktop/abc", *substr;
    const int exists = (substr = strstr(str, "/abc")) && (substr[4] == '\0' || substr[4] == '/');
    printf("%d\n",exists);
    return 0;
}

This should work in all cases.

这应该适用于所有情况。

#5


0  

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

使用strstr(const char *s, const char *t)并包含

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

您可以编写自己的函数,其行为与strstr相同,还可以根据您的需求进行修改

char * str_str(const char *s, const char *t)
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++) 
{
for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++);
if (k > 0 && t[k] == '\0')
return (&s[i]);
}
return NULL;
}

#6


0  

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

如果您过多地使用数组,那么应该包含cstring。因为它有太多的函数,包括查找子字符串。

#1


17  

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).

注意不要偶然地发现一个太短的子字符串(这是开始和结束斜线的目的)。

#2


35  

Use std::string and find.

使用std::string和发现。

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

#3


8  

Example using std::string find method:

示例使用std::string find方法:

#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.

#4


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 /abc followed by a string-terminator '\0'. A naive way to do this would be to check if either /abc/ or /abc\0 are 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\0");
    printf("%d\n",exists);
    return 0;
}

but exists will 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 /abc is a substring, and then see if the character after this substring (indexed using the pointer returned by strstr()) is either a / or a '\0':

但是,即使abc没有被一个空终止符跟踪,它仍然是1。这是因为字符串文字“/abc\0”等同于“/abc”。更好的方法是测试/abc是否是子字符串,然后查看这个子字符串(使用strstr()返回的指针进行索引)后面的字符是A /还是'\0':

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

int main() {
    const char *str = "/user/desktop/abc", *substr;
    const int exists = (substr = strstr(str, "/abc")) && (substr[4] == '\0' || substr[4] == '/');
    printf("%d\n",exists);
    return 0;
}

This should work in all cases.

这应该适用于所有情况。

#5


0  

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

使用strstr(const char *s, const char *t)并包含

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

您可以编写自己的函数,其行为与strstr相同,还可以根据您的需求进行修改

char * str_str(const char *s, const char *t)
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++) 
{
for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++);
if (k > 0 && t[k] == '\0')
return (&s[i]);
}
return NULL;
}

#6


0  

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

如果您过多地使用数组,那么应该包含cstring。因为它有太多的函数,包括查找子字符串。