Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find substring from string?

Tags:

c++

c

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.

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

like image 267
CrazyCoder Avatar asked Nov 02 '12 12:11

CrazyCoder


People also ask

How do you find a substring in a string?

Simple Approach: The idea is to run a loop from start to end and for every index in the given string check whether the sub-string can be formed from that index. This can be done by running a nested loop traversing the given string and in that loop running another loop checking for sub-string from every index.

How can you extract a substring from a given string?

You can extract a substring from a String using the substring() method of the String class to this method you need to pass the start and end indexes of the required substring.

How do I find a substring in a string python?

Python String find() method returns the lowest index or first occurrence of the substring if it is found in a given string. If it is not found, then it returns -1. Parameters: sub: Substring that needs to be searched in the given string.

Which function is used to find the substring of string?

(Search String for Substring) In the C Programming Language, the strstr function searches within the string pointed to by s1 for the string pointed to by s2. It returns a pointer to the first occurrence in s1 of s2.


1 Answers

Use std::string and find.

std::string str = "/user/desktop/abc/post/";
bool exists = str.find("/abc/") != std::string::npos;
like image 145
Luchian Grigore Avatar answered Oct 21 '22 08:10

Luchian Grigore