### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: How to get the quantity of "LineEnding" of a string variable?  (Read 4507 times)

#### Jvan

• Full Member
• Posts: 181
##### How to get the quantity of "LineEnding" of a string variable?
« on: May 13, 2021, 02:26:15 am »
Thanks.

#### dbannon

• Hero Member
• Posts: 1405
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #1 on: May 13, 2021, 02:36:03 am »
That should take the award for shortest post for the day !

Do you mean the size, in bytes ?    if so, its 1 on Unix platforms, 2 on Windows

Or do you mean how many LineEndings there are in a string ? You count then them....

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

#### egsuh

• Hero Member
• Posts: 741
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #2 on: May 13, 2021, 03:13:42 am »
Or do you mean how many string matches with "LineEnding" within a string variable? ^^

#### lucamar

• Hero Member
• Posts: 4017
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #3 on: May 13, 2021, 06:11:24 am »
Code: Pascal  [Select][+][-]
1. uses StrUtils;
2.
3. function LineEndCount(const sv: String): Integer;
4. var
5.   p: Integer;
6. begin
7.   Result := 0;
8.   p := 0
9.   repeat
10.     p := PosEx(LineEnding, sv, p);
11.     if p <> 0 then begin
12.       Inc(Result);
13.       Inc(p, Length(LineEnding));
14.     end;
15.   until p = 0;
16. end;
« Last Edit: May 13, 2021, 06:13:42 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### MarkMLl

• Hero Member
• Posts: 2712
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #4 on: May 13, 2021, 09:37:10 am »
Thanks.

Noting what other people have said and considering the vagueness of "quantity":

Pascal doesn't terminate string variables, so they have no explicit line ending, if by "quantity" you mean "what character is stored?".

The length of the string may be obtained by using Length(), if by "quantity" you mean "how much precedes the line ending?".

The runtimes are aware of the current OS so have a default line ending when writing, see https://www.freepascal.org/docs-html/current/rtl/system/defaulttextlinebreakstyle.html , if by "quantity" you mean "what character is written?".

Next time, ask a sensible question rather than leaving us all having to guess and wasting both your time and ours telling you things you might already know.

MarkMLl
« Last Edit: May 13, 2021, 10:49:23 am by MarkMLl »
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### 440bx

• Hero Member
• Posts: 2368
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #5 on: May 13, 2021, 10:37:56 am »
This post is great.

The question comes somewhat close to making sense but doesn't.  In the meantime well intentioned people who want to help are shooting in the dark.

I'm having trouble getting the strings of a quantity of line endings.

Thanks!
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

#### jamie

• Hero Member
• Posts: 4581
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #6 on: May 13, 2021, 11:25:26 pm »
Seems straight forward to me,...

Create a Stringlist, set the text property from that unknown string
and after that, you can use the COUNT property in the stringlist to determine number of (quantity) Line Endings..

I suppose one could also use Slip specifying the line ending.
etc
Of course you do need to determine what kind of line ending you have, for that you can use the POS to experiment with the line ending pairs to see what you have on the first line.
The only true wisdom is knowing you know nothing

#### Jvan

• Full Member
• Posts: 181
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #7 on: May 14, 2021, 12:51:28 am »
Well, my question was about how many times the "LineEnding" was added to my string variable.

#### dseligo

• Sr. Member
• Posts: 277
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #8 on: May 14, 2021, 12:54:22 am »
Well, my question was about how many times the "LineEnding" was added to my string variable.

Well, Lucamar gave you exact solution in answer #3.

#### Jvan

• Full Member
• Posts: 181
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #9 on: May 14, 2021, 01:06:07 am »
Seems straight forward to me,...

Create a Stringlist, set the text property from that unknown string
and after that, you can use the COUNT property in the stringlist to determine number of (quantity) Line Endings..

I suppose one could also use Slip specifying the line ending.
etc
Of course you do need to determine what kind of line ending you have, for that you can use the POS to experiment with the line ending pairs to see what you have on the first line.

That works, thanks.

#### MarkMLl

• Hero Member
• Posts: 2712
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #10 on: May 14, 2021, 08:51:50 am »
Well, my question was about how many times the "LineEnding" was added to my string variable.

NOW you tell us :-)

Right. So you have a single string variable, which might contain more than one line ending. That's distinguishing from a multi-line file.

Assuming that the lineending matches what's expected by the system (i.e. if you're on a DOS/Windows machine it's <cr><lf> and so on).

Assuming that the lineendings are interleaved with arbitrary text: use Lucamar's solution.

If the lineendings are all at the end of the string, you can optimise that by starting at the end and working towards the beginning and stopping when you no longer have a match. However since LineEnding's length varies depending on the operating system, the effort involved in getting that right will probably be overkill unless the operation is performance-critical.

Don't use Jamie's solution. It's a convenient hack, but has the overhead of allocating a TStringList- a non-trivial operation- and then moving data around rather than simply scanning it in place.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

#### jamie

• Hero Member
• Posts: 4581
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #11 on: May 14, 2021, 01:31:29 pm »
Well, my question was about how many times the "LineEnding" was added to my string variable.

NOW you tell us :-)

Right. So you have a single string variable, which might contain more than one line ending. That's distinguishing from a multi-line file.

Assuming that the lineending matches what's expected by the system (i.e. if you're on a DOS/Windows machine it's <cr><lf> and so on).

Assuming that the lineendings are interleaved with arbitrary text: use Lucamar's solution.

If the lineendings are all at the end of the string, you can optimise that by starting at the end and working towards the beginning and stopping when you no longer have a match. However since LineEnding's length varies depending on the operating system, the effort involved in getting that right will probably be overkill unless the operation is performance-critical.

Don't use Jamie's solution. It's a convenient hack, but has the overhead of allocating a TStringList- a non-trivial operation- and then moving data around rather than simply scanning it in place.

MarkMLl
Yeah, don't use my hacked code of only 3 lines or so, instead use code that turns into a full page on the screen , like this post for example.

Have a good day.
The only true wisdom is knowing you know nothing

#### lucamar

• Hero Member
• Posts: 4017
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #12 on: May 14, 2021, 02:52:40 pm »
Yeah, don't use my hacked code of only 3 lines or so, instead use code that turns into a full page on the screen

You must have a very small screen if a 14 lines function (my code, above) occupies a full-page on it

You've got to relax a little, Jamie: not everybody is out to get you, you know. The point of my code vs. your sugestion is not that your approach is inherently wrong (though see next paragraph) but that using a TStringList just for the sake of counting how many "lines" there are in a string is a little overkill (IMHO, of course).

There is also the small detail that Count will always be off-by-one (except for an empty string): remember, the OP wanted to count line-ends, not lines per se, so he would have to take this into account.

Though, of course, I might be completely wrong in everything... errare humanum est
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!)
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

#### Bart

• Hero Member
• Posts: 4283
##### Re: How to get the quantity of "LineEnding" of a string variable?
« Reply #13 on: May 14, 2021, 02:53:57 pm »
If you LineEndings may vary (so you could have #13#10, a single #13, a sinlge #10, or even worse, combinations of that) you could first use the AdjustLineBreaks procedure on the string, then use Lucamar's solution in answer #3.

If this is a homework assignment, you most likely need not bother.

Bart