Page 1 of 1

Getting first paragraph of a string

Posted: 24 Mar 2020 17:24
by tsolignani
Good evening everybody.

I would need to extract the first paragraph of a string, id est the part before the first couple of line returns (\n\n)

For example:

"We had wanted to leave at 20.

But we left at 22"

What I would like to get is "we had wanted to leave at 20".

I guess I have to use the two line feed, for I realized I sometimes put a space after the full stop, so that I guess I cannot use it...

I know I should use regex but that's still.beyond me.

Any hint?

Thank you, have a nice evening.

Re: Getting first paragraph of a string

Posted: 24 Mar 2020 22:22
by Horschte
You can use split to get the list of paragraphs.

Code: Select all

input = "Paragraph 1\n\nParagraph 2";
paragraphs = split(input, "\n\n");
first = paragraphs[0];
or shorter:

Code: Select all

input = "Paragraph 1\n\nParagraph 2";
first = split(input, "\n\n")[0];

Re: Getting first paragraph of a string

Posted: 25 Mar 2020 15:49
by BoBo
"... for I realized I sometimes put a space after the full stop, ..."
Why not (String) replace full stops with line feeds in the first instance? And/or (String) trim the leading/trailing space chars??

Re: Getting first paragraph of a string

Posted: 26 Mar 2020 16:25
by tsolignani
Horschte wrote:
24 Mar 2020 22:22
You can use split to get the list of paragraphs.
Thank you. I could have thought of that, as a matter of fact ;-)

Thanks.

Re: Getting first paragraph of a string

Posted: 26 Mar 2020 16:26
by tsolignani
BoBo wrote:
25 Mar 2020 15:49
Why not (String) replace full stops with line feeds in the first instance? And/or (String) trim the leading/trailing space chars??
Thank you, a good hint!

THanks.