Recent

Author Topic: Acerca de la función Length.  (Read 6765 times)

AntonioFS

  • New Member
  • *
  • Posts: 12
Acerca de la función Length.
« on: August 20, 2017, 10:38:09 pm »
Hola.

Sirva este comentario para reflejar a quienes somos novatos en FreePascal, un aspecto de la función Length que me ha tenido ocupado un buen rato hasta descubrir la lógica de cálculo con la que no di hasta apercibirme de que el acento en una letra, también cuenta como carácter. Es decir, siendo el programa siguiente:

Var
  Cadena : String[40];
  Contador : Integer;

Begin
  Cadena:='Hola, buen día.';
  Contador:=Length(Cadena);
  WriteLn:=('Longitud de la variable Cadena: ', Contador);
End.


Resulta que me devolvía 16. Conté a saber la de veces el contenido de Cadena y siempre me daba 15, pero claro, es que "día" no tiene tres, sino cuatro caracteres ya que "í" conforma dos: la letra y su acento.

Quede esta observación para despistados como yo. :-) :-)

Saludos cordiales.
Antonio F.S.
« Last Edit: August 20, 2017, 10:44:40 pm by AntonioFS »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Acerca de la función Length.
« Reply #1 on: August 20, 2017, 10:54:05 pm »
Your string contains the "character" *í.
Since Lazarus defaults it's (file) encoding to UTF8, that "character" actually consists of 2 chars (bytes).
And Length() returns the number of chars, it returns 1 more than you expected.

Utf8Length(Cadena) will return the value you expect.

* In this context, by "character" I mean the letter "í" as you see it on screen.
This is not always the same as the type Char in Pascal.

Bart

AntonioFS

  • New Member
  • *
  • Posts: 12
Re: Acerca de la función Length.
« Reply #2 on: August 20, 2017, 11:09:23 pm »
Hola Bart.

Pues acabo de probar Utf8Length(Cadena) y el compilador emite el siguiente mensaje:
Error: Identifier not found "Utf8Length".

Antonio F.S.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Acerca de la función Length.
« Reply #3 on: August 20, 2017, 11:19:38 pm »
It is in unit LazUtf8.

(Note: I don't speak or unerstand the spanish language)

Bart

AntonioFS

  • New Member
  • *
  • Posts: 12
Re: Acerca de la función Length.
« Reply #4 on: August 20, 2017, 11:52:14 pm »
Hello again.
Now it is correct, but first you have to add the LazUtils package in the Project Inspector, otherwise it will still give us a compilation error.

Effectively with Utf8Length (string) gives us the exact number of characters, whether or not they are accented.

Bart, thank you very much for this important note.

Best regards.
Antonio F.S.

------------

Hola de nuevo.
Ahora sí está correcto, pero antes hay que añadir el paquete LazUtils en el Inspector de proyectos, pues de no ser así nos seguirá dando error de compilación.

Efectivamente con Utf8Length(cadena) nos da el número exacto de caracteres, aunque éstos estén o no acentuados.

Bart, muchas gracias por este importante apunte.

Atentamente.
Antonio F.S.

 

TinyPortal © 2005-2018