Getting first paragraph of a string

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Getting first paragraph of a string

Post by tsolignani » 24 Mar 2020 17:24

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.

Horschte
Posts: 56
Joined: 03 Nov 2014 18:00

Re: Getting first paragraph of a string

Post by Horschte » 24 Mar 2020 22:22

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];

BoBo
Posts: 129
Joined: 05 May 2014 12:45

Re: Getting first paragraph of a string

Post by BoBo » 25 Mar 2020 15:49

"... 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??

User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Re: Getting first paragraph of a string

Post by tsolignani » 26 Mar 2020 16:25

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.

User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Re: Getting first paragraph of a string

Post by tsolignani » 26 Mar 2020 16:26

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.

Post Reply