Sto entrando leggermente in confusione (come al solito).
Dunque...
Riesco a leggere correttamente una vocale accentata, ma non a gestirla.
Nell'esempio che allego il programma non ha problemi nel leggere il carattere Car (cui è associata la vocale accentata) né nel "concatenarla" con altri caratteri. Tuttavia (a quanto pare) a ogni vocale accentata vengono associati TRE caratteri, probabilmente si tratta del codice esadecimale (vedi secondo screenshot).
Il fatto è che dopo aver estratto da una stringa una vocale accentata, vorrei un "qualcosa" che mi dica: "La vocale accentata è: à" in modo che possa sottoporla a elaborazione successiva (Tipo: IF Car := 'è' THEN ... eccetera).
P.S. Ho già letto https://forum.lazarus.freepascal.org/index.php/topic,31387.0.html ma purtroppo non è molto chiaro.
Come fare?
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
Var I:Integer;
Car, Parola: String;
begin
Parola:= '';
For I := 1 to Length(Edit1.Text) do
Begin
Car:= Copy(Edit1.Text,I,1);
ShowMessage(Car);
Parola:= Parola + Car;
End;
Label1.Caption:='Parola: ' + Parola + ' Lunghezza: ' + IntToStr(Length(Parola));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Label2.Caption:=('Lunghezza stringa: ' + IntToStr(Length(Edit1.text)));
end;
end.
fai poi attenzione agli operatori
...in modo che possa sottoporla a elaborazione successiva...
(Tipo: IF Car := 'è' THEN ... eccetera).
non puoi usare con
Eccomi. Qui un esempio su come iterare su una stringa con caratteri utf8.
//Inserire nella Uses:
Uses LazUtf8;
procedure TForm1.Button1Click(Sender: TObject);
var tempstr: string;
car: string;
UTtempstr: UnicodeString;
UTcar: Unicodestring;
i: integer;
begin
tempstr := '这些是汉字';
UTtempstr := tempstr;
ShowMessage(tempstr);
for i := 0 to UTF8Length(tempstr)-1 do
begin
car := Utf8Copy(tempstr, i+1, 1);
ShowMessage(car);
//Questo è solo un esempio.
//Si possono usare le funzioni Utf8 per fare la ricerca (PosEx) o altro
if car = '是' then
ShowMessage('Carattere trovato');
end;
ShowMessage('Modo alternativo');
//Ulteriore metodo con le stringhe Unicode
for UTcar in UTtempstr do
ShowMessage(UTcar)
end;
PS: inserito metodo alternativo.
Eccomi. Qui un esempio su come iterare su una stringa con caratteri utf8.
...eccetera
GRANDISSIMO! (Non è che mi poi mandi la fattura per le consulenze, eh?) 🤔
Inserisco qui di seguito il mio modesto contributo (intero listato) e lo screenshot.
A proposito dei messaggi di errore, in molti programmi me ne compaiono spesso ma sembra che nonostante i vari "Warning" e le "Note" tutto funzioni sempre, senza problemi. Mah, non capisco, ma mi adeguo. (cit.)
Grazie ancora!
unit a_2022_11_09_Lazarus_vocali_pas;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, LazUtf8;
// LazUtf8 inserita nella uses per gestire vocali accentate
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
Var I:Integer;
Car, Parola: String;
begin
Parola:= '';
For I := 1 to Length(Edit1.Text) do
Begin
Car:= Copy(Edit1.Text,I,1);
ShowMessage(Car);
Parola:= Parola + Car;
End;
Label1.Caption:='Parola: ' + Parola + ' Lunghezza: ' + IntToStr(Length(Parola));
end;
procedure TForm1.Button2Click(Sender: TObject);
// Gestione vocali accentate
// LazUtf8 inserita nella uses per gestire vocali accentate
var tempstr: string;
car: string;
UTtempstr: UnicodeString;
// UTcar: Unicodestring;
i: integer;
begin
tempstr := edit1.Text;
UTtempstr := tempstr;
ShowMessage(tempstr);
for i := 0 to UTF8Length(tempstr)-1 do
begin
car := Utf8Copy(tempstr, i+1, 1);
//Si possono usare le funzioni Utf8 per fare la ricerca (PosEx) o altro
if (car = 'à') OR (car = 'è') OR (car = 'é') OR (car = 'ì') OR (car = 'ò') Or (car = 'ù') then
// ShowMessage('Carattere trovato');
ListBox1.items.add('Pippo'+car);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Label2.Caption:=('Lunghezza stringa: ' + IntToStr(Length(Edit1.text)));
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
Label3.Caption:=('Stringa selezionata: ' + ListBox1.items.strings[ListBox1.itemindex]);
end;
end.