Forum > General
[Solved] Relationship between high(ashortstring) and length(ashortstring)
Fred vS:
Hello.
This is a question asked in Spanish forum but we (I) dont get the logical.
https://forum.lazarus.freepascal.org/index.php/topic,60504.msg452955.html#msg452955
With this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program cadena;{$mode objfpc}{$H-} // Alias String is ShortStringvar Frase: String;begin Frase := 'Esto es una prueba'; WriteLn(low(Frase)); WriteLn(High(Frase)); WriteLn(Length(Frase)); Readln()end.
This is the result:
--- Code: Bash [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---fred@fred-80m0 ~> ./cadena025518
Why High(Frase) = 255 (and not Length(Frase) - 1) ?
KodeZwerg:
Length() used on a string gives back its size in chars.
Low() and High() give back start and end position for an array.
In your case the string is optimized by compiler to a shortstring.
Reading definition for https://wiki.lazarus.freepascal.org/Shortstring shows me:
--- Quote ---Warning: low(myShortString) returns 0, i. e. the index of the length Byte, not the first character’s index. Likewise, high(myShortString) always returns 255. Use length instead.
--- End quote ---
I hope this helps the spanish forum :-*
Fred vS:
--- Quote from: KodeZwerg on September 10, 2022, 03:56:06 pm ---
--- Quote ---Warning: ... Likewise, high(myShortString) always returns 255. Use length instead.
--- End quote ---
--- End quote ---
Ha, ok, hidden in the bottom, many thanks for the light.
(But, imho, a few exotic to do so, anyway, understood now.)
Arioch:
confuse them yet better :-)
--- Quote ---program cadena;
{$mode objfpc}{$H+} // Alias String is AnsiString
var
Frase: String[100];
begin
Frase := 'Esto es una prueba';
WriteLn(low(Frase));
WriteLn(High(Frase));
WriteLn(Length(Frase));
Readln()
end.
--- End quote ---
Kays:
--- Quote from: KodeZwerg on September 10, 2022, 03:56:06 pm ---Low() and High() give back start and end position for an array.
--- End quote ---
I concur. Any index in the range low(T)..high(T) is valid insofar it will not cause a memory access violation. It doesn’t mean the component at high(T) will be meaningful (has been initialized). To get meaningful content from a shortString use the range 1..length(T) as index.
Navigation
[0] Message Index
[#] Next page