Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you specify what ISN'T a delimiter in std::getline?

I want it to consider anything that isn't an alphabet character to be a delimiter. How can I do this?

like image 273
Marty Avatar asked Jan 29 '26 13:01

Marty


2 Answers

You can't. The default delimiter is \n:

while (std::getline (std::cin, str) // '\n' is implicit

For other delimiters, pass them:

while (std::getline (std::cin, str, ' ') // splits at a single whitespace

However, the delimiter is of type char, thus you can only use one "split-character", but not what not to match.

If your input already happens to be inside a container like std::string, you can use find_first_not_of or find_last_not_of.


In your other question, are you sure you have considered all answers? One uses istream::operator>>(std::istream&, <string>), which will match a sequence of non-whitespace characters.

like image 131
Sebastian Mach Avatar answered Feb 01 '26 03:02

Sebastian Mach


You don't. getline is a simple tool for a simple job. If you need something more complex, then you need to use a more complex tool, like RegEx's or something.

like image 29
Nicol Bolas Avatar answered Feb 01 '26 01:02

Nicol Bolas