Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How to get substring after a character?

For example, if I have

string x = "dog:cat"; 

and I want to extract everything after the ":", and return cat. What would be the way to go about doing this?

like image 380
SKLAK Avatar asked Jan 27 '15 05:01

SKLAK


People also ask

How to split a string by character in C?

In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.

How to access a part of a string in C?

Search for a character in a string - strchr & strrchr The strchr function returns the first occurrence of a character within a string. The strrchr returns the last occurrence of a character within a string. They return a character pointer to the character found, or NULL pointer if the character is not found.

How to make substring from string in C?

C program to fetch substring of a string using strncpy function. char *strncpy (char *destination, const char *source, size_t num); In this program we are using an extra subString character array to store sub-string form input array. We initialize it with null character using memset function.

How do you rest a string after strtok?

strtok remembers the last string it worked with and where it ended. To get the next string, call it again with NULL as first argument.


1 Answers

Try this:

x.substr(x.find(":") + 1);  
like image 194
rcs Avatar answered Oct 02 '22 23:10

rcs