Recent

Author Topic: Search for a spanish charater  (Read 2342 times)

katfo

  • New Member
  • *
  • Posts: 39
Search for a spanish charater
« on: August 09, 2017, 08:29:57 pm »
In my project I am trying to detect spanish special characters such as for example ó, ñ etc.
In order to look at the troubling point I made a simple program. It has on its form only an edit-field and a button. If a character is found in the word to be searched, it shows “found” in the edit-field. Otherwise it shows “not found”. My simple program is the following.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3.  
  4. interface
  5.  
  6. uses
  7.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  8.  
  9. type
  10.     { TForm1 }
  11.  
  12.   TForm1 = class(TForm)
  13.     Button1: TButton;
  14.     Edit1: TEdit;
  15.     procedure Button1Click(Sender: TObject);
  16.   private
  17.     { private declarations }
  18.   public
  19.     { public declarations }
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.   result, word, searchfor: string;
  25.   i:integer;
  26.  
  27. implementation
  28.  {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. begin
  34.   searchfor:='a';
  35.   result:='not found';
  36.   for i:=1 to length(word) do
  37.   if copy(word,i,1)=searchfor then
  38.   begin
  39.      result:='found';
  40.      break
  41.   end;
  42.   edit1.text:=result;
  43. end;
  44.  
  45. begin
  46.  word:='mañana'
  47. end.
  48.  

In the code one put’s the character to be searched in the variable searchfor and then run’s the program. Of course it could be made more elegant but that is not now the point. It detects the letters m, a and n in word “mañana” but do not find ñ. Why is that and how the code should be changed. Thank you in advance for your help.

balazsszekely

  • Guest
Re: Search for a spanish charater
« Reply #1 on: August 09, 2017, 08:40:01 pm »
Hi katfo,

You didn't mention Lazarus/FPC version. OS? Anyway this should work with FPC 3.0.0 up:
Code: Pascal  [Select][+][-]
  1. var
  2.   P: Integer;
  3. begin
  4.   P := Pos('ñ', 'mañana');
  5.   if P > 0 then
  6.     ShowMessage('Found at: ' + IntToStr(P));
  7. end;


katfo

  • New Member
  • *
  • Posts: 39
Re: Search for a spanish charater
« Reply #2 on: August 09, 2017, 09:31:54 pm »
Thank you GetMem, your solution did work. But why my solution didn’t although it works with all the “normal” characters. I have Lazarus 3.6.4 on Win 10.

katfo

  • New Member
  • *
  • Posts: 39
Re: Search for a spanish charater
« Reply #3 on: August 11, 2017, 10:03:08 am »
Hello again. My problem is now solved. When debugging it I stumbled to change my code a bit.  However, I don’t understand why it works now. If I return to my original message and do the following changes in code, it works:

1. On line 34 I change to “searchfor:='ñ';”
2. On line 37 I replace “copy(word,i,1)” with  “copy(word,i,2)”

Why with the normal characters it is sufficient to use “copy(word,i,1)” but with the Spanish special characters “copy(word,i,2)”? Has someone of you any idea of this?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Search for a spanish charater
« Reply #4 on: August 11, 2017, 11:19:28 am »
ANSI characters are encoded as a single byte per character.
Other character sets are encoded as two or more bytes.
So for some character sets you need 3 or 4 bytes per codepoint.
Spanish and other European languages are all encoded within two bytes AFAIK.

magu

  • New Member
  • *
  • Posts: 36
Re: Search for a spanish charater
« Reply #5 on: August 11, 2017, 12:54:46 pm »
In my experience it is better to use the utf8 functions (utf8length, uft8copy) etc since these functions identify the size of the character (one byte or two, etc).

I had a similar problem with Maltese unicode characters, my solution was the following:

Code: Pascal  [Select][+][-]
  1. Const
  2.   MalteseUnicode : Array[0..7] of String = ('Ġ', 'ġ', 'Ħ', 'ħ', 'Ż', 'ż', 'Ċ', 'ċ');
  3.  
  4. Function ArrayReturnIndex(AnArray: Array of string; achar : string) : integer;
  5. // Searches an array for a specific element, and returns its index
  6.   var s: integer;
  7.   begin
  8.     result := Low(AnArray) -1;
  9.     for s := Low(AnArray) to High(AnArray) do
  10.     begin
  11.       if aChar = AnArray[s] then
  12.       begin
  13.         result := s;
  14.         exit;
  15.       end;
  16.     end;
  17.   end;
  18.  
  19. // usage:
  20.  
  21. Procedure SearchForUnicode;
  22. var a,i : integer;
  23.     astring : string;
  24.     unichar : string;
  25. begin
  26.  
  27.   For i := UTF8Length(astring) downto 1 do
  28.   begin
  29.     unichar := UTF8Copy(strTemp,i,1);
  30.  
  31.    if  ArrayReturnIndex(MalteseUnicode,unichar) >= 0 then
  32.    begin
  33.        // you found the character you want...
  34.    end;
  35.   end;
  36.   Result := strTemp;
  37. end;

 

TinyPortal © 2005-2018