Recent

Author Topic: Getting string lenght  (Read 3226 times)

ssliackus

  • Jr. Member
  • **
  • Posts: 65
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: 5275
    • 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

  • Jr. Member
  • **
  • Posts: 65
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: 201
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 »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • 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

  • Jr. Member
  • **
  • Posts: 65
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: 4144
Re: Getting string lenght
« Reply #6 on: September 16, 2018, 11:53:33 pm »
No, dynamic arrays always start at index 0.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
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 »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
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.

 

TinyPortal © 2005-2018