Split string into sentences?

Mar 14, 2016 at 2:59pm
I need a little code to split a phrase/paragraph into sentences.

For example:
"My house is on fire. That should not be happening."
Is splitted into "My house is on fire." and "That should not be happening." and stored into separate strings.
Mar 14, 2016 at 3:04pm
Usually a sentence ends with a full stop '.' (period).
But there might be others, for example '!' or '?'.

You can use find_first_of() to search a string for one of several characters. Then use substr() to extract the substring.

http://www.cplusplus.com/reference/string/string/find_first_of/
http://www.cplusplus.com/reference/string/string/substr/

Mar 14, 2016 at 3:13pm
There is one thing i don't understand about substr().
Does it affect the string (remove the part selected) or would you have to do that sepparately?
Mar 14, 2016 at 3:18pm
No, substr() doesn't change the original string.

You don't actually need to modify the original string, just set the beginning of the next sentence to the first non-whitepace character in the string.

Though if you wanted to remove the substring as well, you could use erase().

http://www.cplusplus.com/reference/string/string/erase/
Mar 14, 2016 at 3:20pm
Im still confused about the "set the begining of the next sentence to the first non-whitespace character". Could you please provide a little code example?
Mar 14, 2016 at 3:50pm
Here's a partial example. It shows some of the ideas needed.
1
2
3
4
5
6
7
8
9
10
11
12
13
    string text = "My house is on fire! That should not be happening.";

    size_t start = 0;
    // look for end of sentence
    size_t finish = text.find_first_of(".!?", start);
    
    if (finish != string::npos)
    {
        // end of sentence was found, do something here.

        // now find start of next sentence
        start = text.find_first_not_of(" \t\n", finish+1);
    }


Mar 14, 2016 at 5:01pm
Oh, thakns. That helped.
Topic archived. No new replies allowed.