Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ split string with space and punctuation chars

Tags:

c++

string

I wanna split an string using C++ which contains spaces and punctuations.

e.g. str = "This is a dog; A very good one."

I wanna get "This" "is" "a" "dog" "A" "very" "good" "one" 1 by 1.

It's quite easy with only one delimiter using getline but I don't know all the delimiters. It can be any punctuation chars.

Note: I don't wanna use Boost!

like image 748
MBZ Avatar asked Feb 22 '23 10:02

MBZ


1 Answers

Use std::find_if() with a lambda to find the delimiter.

auto it = std::find_if(str.begin(), str.end(), [] (const char element) -> bool {
                       return std::isspace(element) || std::ispunct(element);})
like image 54
NFRCR Avatar answered Feb 23 '23 22:02

NFRCR