How about letting the IDE deal with counting array elements instead of the compiler doing it?
Something like https://forum.lazarus.freepascal.org/index.php?topic=27186.15
That is a nice idea but, it has one major downside. If a string is changed (made smaller or longer) and the programmer forgets to tell the IDE to "update" the dependent locations then, things are no longer in sync. Additionally, it's difficult to trust the IDE to do those things correctly since the IDE usually, unlike the compiler, has a limited view of the entire program.
If you do a shortstring (max len 255) and sizeof(), maybe that will do. (not tested, and if it works may be one extra)
const foo = shortstring('abc');
With ansistring that will not work, since sizeof(ansistring) = sizeof(pointer).
What would you do want to do with the value, if you could get it?
Unfortunately, even with a shortstring, the compiler does not allow using the length of the string at compile time. For instance, it will not accept "const Mylength = mystring[0];" or "const mylength = length(mystring);". Taking sizeof(shortstring) will yield a value that is the size of a shortstring, not the size/character count of the string it holds (which is reasonable since, as you know, that is not a constant, it's just an initialized variable.)
As far as what I'd do with it, it would be a safe and convenient way of defining the size of dependent types just as it is commonly done with regular data types, e.g, "somecharbuffer = array[0..2 * size(byte)] of char" to define a buffer that can hold a (single byte) character for each nibble in a byte plus the null terminator, change "byte" to any other ordinal type to have a buffer that snuggly holds the type converted to "array of char" (hex conversion). If the programmer needs a buffer to hold a converted qword, just change "byte" to "qword" and everything is updated automatically by the compiler. That's just an example, as you know, there are countless examples where knowing the size of an item at compile time can be very useful in the definition of dependent types and writing code that is automatically updated by the compiler if the target type (in this case byte) changes in the future.
Specifically with null terminated strings. If you're going to build at run time a "composite" string made of various constant strings and you know at compile time the character counts of each string that will make up the composite string then, it is possible to declare a buffer type that is something along the lines of "TMyBuffer : array[0..sizeofastring + sizeofanotherstring + sizeofyetanotherstring] of char;". On one side, it spares the programmer from having to define a maximum size, thereby ensuring that the buffer is always large enough to accommodate the resulting string. If a routine builds a half dozen of these strings and they reside on the stack before being output, It can make the difference between allocating a few hundred bytes instead of a "max_buffer_size" for each string and still running the risk that there may be one combination that causes a buffer overflow thereby corrupting the stack.
Basically, it allows to write cleaner and safer code when strings (char arrays) are involved.
It would be nice if it were available, particularly considering that the compiler has the information but, Length and strlen, it is.