* * *

Author Topic: Getting string lenght  (Read 292 times)

ssliackus

  • New member
  • *
  • Posts: 32
Getting string lenght
« on: September 16, 2018, 09:50:08 pm »
Hi there,

I am using high() to get string length and to iterate it trough loop, but I got two different lengths of the string (see image).
Why is that so? I was expecting to have the same value in both cases.

Thanks

Bart

  • Hero Member
  • *****
  • Posts: 3089
    • Bart en Mariska's Webstek
Re: Getting string lenght
« Reply #1 on: September 16, 2018, 09:59:04 pm »
Life is simple: Length(MySTring).
Pascal string indexes always start with 1, so you for loop can go from 1 to Length(MyString).

Bart

ssliackus

  • New member
  • *
  • Posts: 32
Re: Getting string lenght
« Reply #2 on: September 16, 2018, 10:21:06 pm »
Well...thank you for help.
But for me that is kind of inconsistency, in my opinion. If usually arrays start with 0, then string is no different array, why it has to start with 1? Also, I would expect the same command (I mean  high() ) with the same given arguments produce the same result, but if plain executed it gives 255, if in loop it gives something else. Not sure if that is right.....
Thanks anyway

fred

  • Full Member
  • ***
  • Posts: 103
Re: Getting string lenght
« Reply #3 on: September 16, 2018, 10:25:25 pm »
I suppose you use ShortString which are 255 bytes.
The first byte is the used length in your case 9 chars.
When you write low(mystring) you write #9 which is a tab character what shows as many spaces after your 'itself:
See http://wiki.freepascal.org/String
« Last Edit: September 16, 2018, 10:29:08 pm by fred »
Started with OmegaSoft Pascal on OS-9/68k , now Lazarus 1.8.4 / FPC 3.0.4 on Windows 7

Bart

  • Hero Member
  • *****
  • Posts: 3089
    • Bart en Mariska's Webstek
Re: Getting string lenght
« Reply #4 on: September 16, 2018, 10:39:31 pm »
But for me that is kind of inconsistency, in my opinion. If usually arrays start with 0, then string is no different array, why it has to start with 1?

Please ask professor Wirth, who invented Pascal

Also, I would expect the same command (I mean  high() ) with the same given arguments produce the same result, but if plain executed it gives 255, if in loop it gives something else. Not sure if that is right.....

Your program does not specify Delphi mode, nor does it specify {$H+} (longstrings on).
So your strings are ShortStrings, which have a size of 256 bytes (0..255), always.
So Low(ShortString) will be 0, High(ShortString) will be 255.

For AnsiStrings Low() will will be 1, High() will be the same as Length().

Code: Pascal  [Select]
  1. {$H+}
  2.  
  3. var
  4.   A: String;
  5.   S: ShortString;
  6. begin
  7.   A := 'foo';
  8.   S := 'foo';
  9.   writeln('Low(A)=',Low(A),', High(A)=',High(A));
  10.   writeln('Low(S)=',Low(S),', High(S)=',High(S));
  11.   A := '';
  12.   S := '';
  13.   writeln('Low(A)=',Low(A),', High(A)=',High(A));
  14.   writeln('Low(S)=',Low(S),', High(S)=',High(S));
  15. end.

Outputs
Code: [Select]
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
Low(A)=1, High(A)=3
Low(S)=0, High(S)=255
Low(A)=1, High(A)=0
Low(S)=0, High(S)=255

Length() also works on dynamic arrays.

Notice that arrays can start with any index, and Low/High will still give you the correct answers.

Bart

ssliackus

  • New member
  • *
  • Posts: 32
Re: Getting string lenght
« Reply #5 on: September 16, 2018, 11:43:10 pm »
Thanks, Bart, that explains a lot. So I am using aka old version of string that does not work like I expected.  Now it works perfeclty, thanks.
As for dynamic arrays. Can they have a custom range? No problems with static

a : array [1..15] of integer;

but is it possible to set custom ranges for dynamic arrays?

Thanks
« Last Edit: September 16, 2018, 11:51:14 pm by ssliackus »

howardpc

  • Hero Member
  • *****
  • Posts: 2752
Re: Getting string lenght
« Reply #6 on: September 16, 2018, 11:53:33 pm »
No, dynamic arrays always start at index 0.

lucamar

  • Full Member
  • ***
  • Posts: 196
Re: Getting string lenght
« Reply #7 on: September 16, 2018, 11:58:13 pm »
[....] but is it possible to set custom ranges for dynamic arrays?

No, because the range is not known at compile time; they always start at zero, as stated in the documentation
« Last Edit: September 17, 2018, 12:12:57 am by lucamar »
Been there, done that ... barely kept the timelines.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/fpc 3.0.4 - Ubuntu 10, Kubuntu 14, Windows XP SP3 (Home & Prof.) and various DOS incarnations.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus