* * *

Author Topic: [SOLVED] - Maybe a problem with .length :S:S  (Read 599 times)

esvignolo

  • Full Member
  • ***
  • Posts: 129
  • Using FPC in Windows, Linux, Macos
[SOLVED] - Maybe a problem with .length :S:S
« on: March 06, 2017, 06:10:06 pm »
Hello! I have a question with the handling of strings. Punctually, the length of the same with accented characters.
Please look at attachament.

In the attachment you can see a screenshot, from the code below. Where there are two TEdit with the same text (with the difference that in one of them, this one with accents), but the length of the same is different, I thought that could be the codification, but both are in UTF8.

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, LConvEncoding;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Edit2: TEdit;
  17.     Label1: TLabel;
  18.     Label2: TLabel;
  19.     procedure Edit1Change(Sender: TObject);
  20.     procedure Edit2Change(Sender: TObject);
  21.     procedure FormShow(Sender: TObject);
  22.   private
  23.  
  24.   public
  25.  
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.Edit1Change(Sender: TObject);
  38.  
  39. begin
  40.    Label1.Caption:='Length: '+intToStr(Length(edit1.Text))+'  GuessEncoding: '+GuessEncoding(edit1.Text);
  41. end;
  42.  
  43.  
  44. procedure TForm1.Edit2Change(Sender: TObject);
  45. begin
  46.     Label2.Caption:='Length: '+intToStr(Length(edit2.Text))+'  GuessEncoding: '+GuessEncoding(edit2.Text);
  47. end;
  48.  
  49. procedure TForm1.FormShow(Sender: TObject);
  50. begin
  51.     Edit1Change(self);
  52.     Edit2Change(self);
  53. end;
  54.  
  55. end.
  56.  
  57.  


Thanks!
« Last Edit: March 07, 2017, 04:51:57 pm by esvignolo »

lainz

  • Hero Member
  • *****
  • Posts: 2001
Re: Maybe a problem with .length :S:S -- SOLVED
« Reply #1 on: March 06, 2017, 06:22:14 pm »
The solution was changing Length with UTF8Length of the unit LazUTF8. If there's a better way please comment.

Also a question is if any other methods need to use UTF8 version of them.

esvignolo

  • Full Member
  • ***
  • Posts: 129
  • Using FPC in Windows, Linux, Macos
Re: Maybe a problem with .length :S:S -- SOLVED
« Reply #2 on: March 06, 2017, 06:25:49 pm »
Thanks to @Lainz to clarify me.

The .length property is in bytes, if u want know the length in char u must use UTF8Length (in LazUTF8 unit)

Here is solved:

Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, LConvEncoding, LazUTF8;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Edit2: TEdit;
  17.     Label1: TLabel;
  18.     Label2: TLabel;
  19.     procedure Edit1Change(Sender: TObject);
  20.     procedure Edit2Change(Sender: TObject);
  21.     procedure FormShow(Sender: TObject);
  22.   private
  23.  
  24.   public
  25.  
  26.   end;
  27.  
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.Edit1Change(Sender: TObject);
  39.  
  40. begin
  41.    Label1.Caption:='Length: '+intToStr(UTF8Length(edit1.Text))+'  GuessEncoding: '+GuessEncoding(edit1.Text);
  42. end;
  43.  
  44.  
  45. procedure TForm1.Edit2Change(Sender: TObject);
  46. begin
  47.     Label2.Caption:='Length: '+intToStr(UTF8Length(edit2.Text))+'  GuessEncoding: '+GuessEncoding(edit2.Text);
  48. end;
  49.  
  50. procedure TForm1.FormShow(Sender: TObject);
  51. begin
  52.     Edit1Change(self);
  53.     Edit2Change(self);
  54. end;
  55.  
  56. end.
  57.  
  58.  


 

Recent

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