Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse delimited string

Tags:

c++

string

char

How can I get :

connect
100
username
example

from this string:

ngg://connect>100/username>example/
like image 801
user525717 Avatar asked Dec 05 '25 19:12

user525717


2 Answers

Using std::string::find with arguments "/" and ">" and std::string::substr with the found indexes.

This is a good start.

like image 137
Luchian Grigore Avatar answered Dec 08 '25 10:12

Luchian Grigore


Adding an answer with strtok for the sake of diversity:

char str[] = "ngg://connect>100/username>example/";
char *s = strtok(str, ">/");
std::vector<std::string> tokens;
while (s = strtok(NULL, ">/"))
    tokens.push_back(std::string(s));

This will split the string str into the desired tokens (discarding the first ngg:, like in the question).
Here's a working example of this code.

like image 28
Eitan T Avatar answered Dec 08 '25 10:12

Eitan T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!