Recent

Author Topic: [Solved] StringReplace and specialchars  (Read 3887 times)

DanishMale

  • Jr. Member
  • **
  • Posts: 73
[Solved] StringReplace and specialchars
« on: August 30, 2016, 03:37:48 am »
Hi all,

First to the makers of Lazarus .. GREAT JOB!

Now, when I try to perform at a StringReplace on ß it also converts all my words containing ss, which it I don't want it to do. How can I create a work around??

e.g.   Converting ß -> ß but NOT ss
« Last Edit: August 30, 2016, 11:39:14 pm by DanishMale »
Lazarus 2.2.4 x64 | Windows 10 x64 | Windows Server 2019 x64 | OpenVix 5.4 (Linux) | MySQL Community Server 8.0 x64 | MariaDB 10.5.8 x64 | SQLite 3.40.0 x64 | PostgresSQL 13.1 x64

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: StringReplace and specialchars
« Reply #1 on: August 30, 2016, 03:25:37 pm »
If you use the rfIgnoreCase flag in StringReplace then first both string parameters get uppercased before replacement takes place.
Unfortunately, the uppercase of "ringel-S" is "SS", and the lowercasing will obviously result in "ss".

So, in this case do the replacing of lowercase ß first (exclude the rfIgnoreCase), and then do the rest of the conversion in a second step.
Untested:

Code: [Select]
  S :=  'Ossenkoppeler  Straße 60 ESSIG';
  S := Utf8StringReplace(S, 'ß', ' ß', [rfReplaceAll]);
  //if you want to replace ss with xx:, note that this is now safe since there is no more Ringel-S in S
  S := Utf8Stringreplace(S, 'ss' 'xx', [rfReplaceAll], rfIgnoreCase);
  //if you want other coversions, like pp -> qq
  S := Utf8Stringreplace(S, 'pp' 'qq', [rfReplaceAll, rfIgnoreCase]);

Bart

DanishMale

  • Jr. Member
  • **
  • Posts: 73
Re: [Solved] StringReplace and specialchars
« Reply #2 on: August 30, 2016, 11:39:42 pm »
Thx Bart... just what I was looking for  :D

Just another additional question...

How do I compare that f.ex. père equals pere in pascal without converting è to e ?

e.g. if POS('mon pére est au Danemark','pere') > 0 then Label1.Caption := 'Daddy found!!!';
« Last Edit: August 31, 2016, 02:44:05 am by DanishMale »
Lazarus 2.2.4 x64 | Windows 10 x64 | Windows Server 2019 x64 | OpenVix 5.4 (Linux) | MySQL Community Server 8.0 x64 | MariaDB 10.5.8 x64 | SQLite 3.40.0 x64 | PostgresSQL 13.1 x64

Fungus

  • Sr. Member
  • ****
  • Posts: 353
Re: [Solved] StringReplace and specialchars
« Reply #3 on: August 31, 2016, 01:10:49 pm »
I do not think a phonetic string search routine exists, but you could bodge one yourself - or you could use regular expressions which are quite powerfull if you understand how to use them. This example will find both pére and pere:

Code: Pascal  [Select][+][-]
  1. var S, R: String;
  2. S:= 'mon pére est au Danemark';
  3. R:= '';
  4. with TRegExpr.Create('p(e|é)re') do try
  5.    ModifierG:= True; //Global search
  6.    ModifierI:= True; //Case insensitive
  7.    if Exec(S) then repeat
  8.      R:= R + 'pere or pére found : ' + IntToStr(MatchPos[0]) + ' (' + Match[0] + ')'#13#10;
  9.    until not ExecNext;
  10. finally
  11.   Free; //TRegExpr
  12. end;
  13. ShowMessage(R);

God fornøjelse ;-)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [Solved] StringReplace and specialchars
« Reply #4 on: August 31, 2016, 02:00:51 pm »
FPC's RTL has several Soundex functions. So you could also try some variation on the following:

Code: Pascal  [Select][+][-]
  1. uses strutils;
  2.  
  3. function FoundSoundsLike(const aWord, aLineToSearch: string; out aWordPos: integer): boolean;
  4. var
  5.   sl: TStringList;
  6.   i: integer;
  7. begin
  8.   Result:=False;
  9.   if (aWord='') or (aLineToSearch='') then
  10.     Exit;
  11.   sl:=TStringList.Create;
  12.   try
  13.     sl.CommaText:=aLineToSearch;
  14.     for i:=0 to sl.Count-1 do
  15.       if SoundexSimilar(aWord, sl[i]) then begin
  16.         aWordPos:=Succ(i);
  17.         Exit(True);
  18.       end;
  19.   finally
  20.     sl.Free;
  21.   end;
  22. end;      

DanishMale

  • Jr. Member
  • **
  • Posts: 73
Re: [Solved] StringReplace and specialchars
« Reply #5 on: September 01, 2016, 09:34:57 am »
@ Fungus and howardpc, thx for your answers I'll look more into it hence you have given me a starting point to achieve my idea  :D
Lazarus 2.2.4 x64 | Windows 10 x64 | Windows Server 2019 x64 | OpenVix 5.4 (Linux) | MySQL Community Server 8.0 x64 | MariaDB 10.5.8 x64 | SQLite 3.40.0 x64 | PostgresSQL 13.1 x64

 

TinyPortal © 2005-2018