Recent

Author Topic: [Solved] i can't get char "é"?  (Read 4747 times)

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
[Solved] i can't get char "é"?
« on: January 08, 2019, 07:11:12 pm »
Hi

this code don't work!
why?
help me please

Code: Pascal  [Select][+][-]
  1. function Nasal(tStr_2:String):String;
  2. var
  3.   ch :Char;
  4. begin
  5.   ch:= tStr_2[1];//   tStr_2 := "étudier"
  6.   if (ch = 'é')then begin
  7.     ShowMessage('OK  '+ch);
  8.     Result := ch;
  9.     end;
  10. end;  
  11.  

when i use this code , "ch" always is ''(empty)??

how can i get char "é"?
« Last Edit: January 08, 2019, 08:57:23 pm by majid.ebru »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: i can't get char "é"?
« Reply #1 on: January 08, 2019, 07:22:20 pm »
Almost:
Code: Pascal  [Select][+][-]
  1.     function Nasal(tStr_2:String):String;
  2.     var
  3.       ch :String;
  4.     begin
  5.       ch:= UTF8Copy(tStr_2,1,1);//   tStr_2 := "étudier"
  6.       if (ch = 'é') then begin
  7.         ShowMessage('OK  '+ch);
  8.         Result := ch;
  9.         end;
  10.     end;

Edit:
é is *not* one byte. Type Char in your code holds one byte only (default settings).

Almost, because é could be written in other ways:
Letter e and Combining Acute Accent
e and U+0301
« Last Edit: January 08, 2019, 07:35:31 pm by engkin »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: i can't get char "é"?
« Reply #2 on: January 08, 2019, 07:40:05 pm »
Or like this:
Code: Pascal  [Select][+][-]
  1. function Nasal(tStr_2:String): String;
  2. begin
  3.   Result := '';
  4.   if tStr_2.StartsWith('é')then begin
  5.     Result := LeftStr(TStr_2, 1);
  6.     ShowMessage('OK  ' + Result);
  7.   end;
  8. end;

Not tested yet ... a moment, please ... Yes, it works.
« Last Edit: January 08, 2019, 07:59:46 pm 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.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: i can't get char "é"?
« Reply #3 on: January 08, 2019, 07:51:27 pm »
Not tested yet ... a moment, please ...

LeftStr(TStr_2, 1) takes one byte from a two byte character. I don't think this works. Maybe UTF8LeftStr? But again, almost.
« Last Edit: January 08, 2019, 07:55:56 pm by engkin »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: i can't get char "é"?
« Reply #4 on: January 08, 2019, 08:04:30 pm »
LeftStr(TStr_2, 1) takes one byte from a two byte character. I don't think this works. Maybe UTF8LeftStr? But again, almost.

In my FPC (vanila 3.0.4) it takes 1 character. Just try it:
  AStr := LeftStr('étudier', 1);
  ShowMessage(IntToStr(Length(AStr)));

shows "2" here.
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.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: i can't get char "é"?
« Reply #5 on: January 08, 2019, 08:14:00 pm »
LeftStr(TStr_2, 1) takes one byte from a two byte character. I don't think this works. Maybe UTF8LeftStr? But again, almost.

In my FPC (vanila 3.0.4) it takes 1 character. Just try it:
  AStr := LeftStr('étudier', 1);
  ShowMessage(IntToStr(Length(AStr)));

shows "2" here.

This code prints 1 on Win32 FPC3.0.4:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes, LazUTF8, SysUtils
  10.   { you can add units after this };
  11.  
  12. var
  13.   AStr: String;
  14. begin
  15.   AStr := LeftStr('étudier', 1);
  16.   WriteLn(IntToStr(Length(AStr)));
  17.  
  18.   ReadLn;
  19. end.

and:
Code: Pascal  [Select][+][-]
  1. function LeftStr(const S: string; Count: integer): string;
  2. begin
  3.   result := Copy(S, 1, Count);
  4. end ;

Since Count is 1, why would it copy 2 on your side?
« Last Edit: January 08, 2019, 08:16:58 pm by engkin »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: i can't get char "é"?
« Reply #6 on: January 08, 2019, 08:30:41 pm »
and:
Code: Pascal  [Select][+][-]
  1. function LeftStr(const S: string; Count: integer): string;
  2. begin
  3.   result := Copy(S, 1, Count);
  4. end ;

Since Count is 1, why would it copy 2 on your side?

I don't know, since it's not so easy to follow Copy() to the used implementation; all I know is that here it shows "2" ... maybe a difference in source encoding?  But note that both are described in the docs as copyig characters. Anyway, no harm in using the corresponding UTF8xxx functions.

ETA: Well, hell: Copy is a built-in, like WriteLn, SetLength and a long etc. Who knows what it really does! :)
« Last Edit: January 08, 2019, 08:42:44 pm 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.

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: i can't get char "é"?
« Reply #7 on: January 08, 2019, 08:55:27 pm »
Or like this:
Code: Pascal  [Select][+][-]
  1. function Nasal(tStr_2:String): String;
  2. begin
  3.   Result := '';
  4.   if tStr_2.StartsWith('é')then begin
  5.     Result := LeftStr(TStr_2, 1);
  6.     ShowMessage('OK  ' + Result);
  7.   end;
  8. end;

Not tested yet ... a moment, please ... Yes, it works.

@lucamar

thank you

i used your code :

Code: Pascal  [Select][+][-]
  1. tStr_2.StartsWith('é')
  2.  
« Last Edit: January 08, 2019, 09:07:59 pm by majid.ebru »

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: [Solved] i can't get char "é"?
« Reply #8 on: January 20, 2019, 07:05:20 pm »
sorry

now

i can't get(or check) "ê"?

why????

Code: Pascal  [Select][+][-]
  1.  
  2. function isinAry(tStr:UTF8String;tAry:array of UTF8String):Boolean;
  3. var
  4.   i : Integer;
  5. begin
  6.   Result := False;
  7.   for i := 0 to Length(tAry) - 1 do begin
  8.     if(tStr = (tAry[i]))then begin // did not work ?!?!?
  9.       Result := True;
  10.       Break;
  11.     end;
  12.   end;
  13. end;
  14.  
  15. procedure TF1.P1_B_2Click(Sender: TObject);
  16. var
  17.   Voule_V01 : array[0..05] of UTF8String = ('a','e','é','ê','h','o');
  18.   t_Str_2 : UTF8String;
  19. begin
  20.   t_Str_2 := 'êtes ';
  21.   if(isinAry((t_Str_2[1]),Voule_V01))then
  22.      ShowMessage('OK');          
  23. end;
  24.  
  25.  

please help me :'( :'( :'(

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [Solved] i can't get char "é"?
« Reply #9 on: January 20, 2019, 07:18:55 pm »
Try this:

Code: Pascal  [Select][+][-]
  1. function isinAry(tStr:UTF8String;tAry:array of UTF8String):Boolean;
  2. var
  3.   i : Integer;
  4. begin
  5.   Result := False;
  6.   for i := Low(tAry) to High(tAry) do begin
  7.     Result := UTF8Pos(tAry[i], tStr) > 0;
  8.     {or, if you want to check only the first character:
  9.     Result := tStr.StartsWith(tAry[i]);
  10.     }
  11.     if Result then
  12.       Break;
  13.   end;
  14. end;
  15.  
  16. procedure TF1.P1_B_2Click(Sender: TObject);
  17. var
  18.   Voule_V01 : array[0..5] of UTF8String = ('a','e','é','ê','h','o');
  19.   t_Str_2 : UTF8String;
  20. begin
  21.   t_Str_2 := 'êtes ';
  22.   if isinAry((t_Str_2, Voule_V01) then
  23.      ShowMessage('OK');          
  24. end;

Your code doesn't works because you're passing a single byte to be compared with an UTF8 character, which would work only for the standard ASCII characters #32..#127
« Last Edit: January 20, 2019, 07:33:56 pm 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.

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: [Solved] i can't get char "é"?
« Reply #10 on: January 20, 2019, 07:24:43 pm »
Try this:

Code: Pascal  [Select][+][-]
  1. function isinAry(tStr:UTF8String;tAry:array of UTF8String):Boolean;
  2. var
  3.   i : Integer;
  4. begin
  5.   Result := False;
  6.   for i := Low(tAry) to High(tAry) do begin
  7.     Result := UTF8Pos(tAry[i], tStr) > 0;
  8.     if Result then
  9.       Break;
  10.   end;
  11. end;

it worked

sorry i didn't see your code:
Code: Pascal  [Select][+][-]
  1. if isinAry((t_Str_2, Voule_V01) then

thankyou but(next post...)
« Last Edit: January 20, 2019, 07:41:36 pm by majid.ebru »

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: [Solved] i can't get char "é"?
« Reply #11 on: January 20, 2019, 07:32:24 pm »
how can i show first char???

i can't show it

Code: Pascal  [Select][+][-]
  1. procedure TF1.P1_B_2Click(Sender: TObject);
  2. var
  3.   t_Str_2 : UTF8String;
  4. begin
  5.   t_Str_2 := 'êtes ';
  6.   ShowMessage(t_Str_2+LineEnding+'"'+t_Str_2[1]+'"');          
  7. end;
  8.  

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: [Solved] i can't get char "é"?
« Reply #12 on: January 20, 2019, 07:54:41 pm »
how can i show first char???
Code: Pascal  [Select][+][-]
  1. uses LazUnicode;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   S: string;
  6. begin
  7.   S := 'êtes ';
  8.   ShowMessage(S + LineEnding + '"' + CodePointCopy(S, 1, 1) + '"');
  9. end;

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: [Solved] i can't get char "é"?
« Reply #13 on: January 20, 2019, 08:16:02 pm »
Try this:

Code: Pascal  [Select][+][-]
  1. function isinAry(tStr:UTF8String;tAry:array of UTF8String):Boolean;
  2. var
  3.   i : Integer;
  4. begin
  5.   Result := False;
  6.   for i := Low(tAry) to High(tAry) do begin
  7.     Result := UTF8Pos(tAry[i], tStr) > 0;
  8.     {or, if you want to check only the first character:
  9.     Result := tStr.StartsWith(tAry[i]);
  10.     }
  11.     if Result then
  12.       Break;
  13.   end;
  14. end;
  15.  
  16. procedure TF1.P1_B_2Click(Sender: TObject);
  17. var
  18.   Voule_V01 : array[0..5] of UTF8String = ('a','e','é','ê','h','o');
  19.   t_Str_2 : UTF8String;
  20. begin
  21.   t_Str_2 := 'êtes ';
  22.   if isinAry((t_Str_2, Voule_V01) then
  23.      ShowMessage('OK');          
  24. end;

Your code doesn't works because you're passing a single byte to be compared with an UTF8 character, which would work only for the standard ASCII characters #32..#127


so sorry agian

your code didn't work always.
Code: Pascal  [Select][+][-]
  1.  Result := UTF8Pos(tAry[i], tStr) > 0;
  2.  

i should send exact first char of "t_Str_2[1]"

in my program some times i send a word and some times i send a char to function

Code: Pascal  [Select][+][-]
  1.   if(isinAry((t_Str_2[1]),Voule_V01))then

majid.ebru

  • Hero Member
  • *****
  • Posts: 502
Re: [Solved] i can't get char "é"?
« Reply #14 on: January 20, 2019, 08:23:11 pm »
how can i show first char???
Code: Pascal  [Select][+][-]
  1. uses LazUnicode;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   S: string;
  6. begin
  7.   S := 'êtes ';
  8.   ShowMessage(S + LineEnding + '"' + CodePointCopy(S, 1, 1) + '"');
  9. end;

thank you

it worked

 

TinyPortal © 2005-2018