Recent

Author Topic: stringreplace for the copyright symbol  (Read 1687 times)

w click

  • Full Member
  • ***
  • Posts: 183
stringreplace for the copyright symbol
« on: March 09, 2022, 03:14:06 pm »
I'm trying to replace '©' with '©' (and a lot of other characters) in a memo from a list in another memo.  It's just not doing anything.  t1 and t2 are the two strings in question.  Can anyone help?

Quote
if memoreplacements.lines.count>0 then for j:=0 to memoreplacements.lines.count-1 do begin
    t:=memoreplacements.lines[j];
    t1:=wordfrom(t,1);
    t2:=wordfrom(t,2);
    memobody.text:=stringreplace(memobody.text,t1,t2,[rfreplaceall]);
  end;

wp

  • Hero Member
  • *****
  • Posts: 12805
Re: stringreplace for the copyright symbol
« Reply #1 on: March 09, 2022, 03:18:31 pm »
What is wordfrom()?

Zvoni

  • Hero Member
  • *****
  • Posts: 2968
Re: stringreplace for the copyright symbol
« Reply #2 on: March 09, 2022, 03:25:03 pm »
What is wordfrom()?
at a guess... wordfrom(t,1) --> first word from t and so on

Looks like he wants to use a Dictionary... *hinthint*

EDIT: considering memo.lines is a TStrings...... why not use the memo.lines in "key/value"-mode?

the memo.lines must be added as "@ = &copy"
Code: Pascal  [Select][+][-]
  1. if memoreplacements.lines.count>0 then
  2.   for j:=0 to memoreplacements.lines.count-1 do
  3.     memobody.text:=stringreplace(memobody.text,memoreplacements.lines.Names[j],memoreplacements.lines.ValueFromIndex[j],[rfreplaceall]);
  4.  
or to stay in his code
Code: Pascal  [Select][+][-]
  1. if memoreplacements.lines.count>0 then
  2.   for j:=0 to memoreplacements.lines.count-1 do begin  
  3.     t1:=memoreplacements.lines.Names[j];
  4.     t2:=memoreplacements.lines.ValueFromIndex[j];
  5.     memobody.text:=stringreplace(memobody.text,t1,t2,[rfreplaceall]);
  6.   end;
  7.  
« Last Edit: March 09, 2022, 03:36:51 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

w click

  • Full Member
  • ***
  • Posts: 183
Re: stringreplace for the copyright symbol
« Reply #3 on: March 09, 2022, 06:24:08 pm »
Oops, sorry.  Wordfrom is excactly as you deduced.  I've been using it so long, I'd forgotten it wasn't standard.

I tried
Quote
if memoreplacements.lines.count>0 then
  for j:=0 to memoreplacements.lines.count-1 do begin 
    t1:=memoreplacements.lines.Names[j];
    t2:=memoreplacements.lines.ValueFromIndex[j];
    memobody.text:=stringreplace(memobody.text,t1,t2,[rfreplaceall]);
  end;

but it didn't work.  I should also have pointed out that my memo of replacements is just a list of symbol, space and replacement, like this:
Quote
© ©
° °
à à

wp

  • Hero Member
  • *****
  • Posts: 12805
Re: stringreplace for the copyright symbol
« Reply #4 on: March 09, 2022, 06:52:04 pm »
The code basically is correct. However, I would not call Memo.Lines.Text all the time because this is a very complex operation, and I could imagine that this is the cause of your issue. Rather than that, use a temporary string variable to collect the converted text. See the project in the attachment.

Zvoni

  • Hero Member
  • *****
  • Posts: 2968
Re: stringreplace for the copyright symbol
« Reply #5 on: March 09, 2022, 10:04:21 pm »
Oops, sorry.  Wordfrom is excactly as you deduced.  I've been using it so long, I'd forgotten it wasn't standard.

I tried
Quote
if memoreplacements.lines.count>0 then
  for j:=0 to memoreplacements.lines.count-1 do begin 
    t1:=memoreplacements.lines.Names[j];
    t2:=memoreplacements.lines.ValueFromIndex[j];
    memobody.text:=stringreplace(memobody.text,t1,t2,[rfreplaceall]);
  end;

but it didn't work.  I should also have pointed out that my memo of replacements is just a list of symbol, space and replacement, like this:
Quote
© ©
° °
à à
You‘re missing the „=„-symbol
Look again at my answer
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

w click

  • Full Member
  • ***
  • Posts: 183
Re: stringreplace for the copyright symbol
« Reply #6 on: March 10, 2022, 12:43:07 pm »
Many thanks for people's help.  Sorry, Zvoni, couldn't see the "=" sign you meant.

I have made progress and do not understand why it now works.  I went back to my original method, put in a few diagnostics and noticed that it was changing some of them, just not the copyright symbol.  So, I added after "© ©" with another "© &test;" – which worked!  So I deleted the first one and changed "test" to "copy" and that worked.  So, the replacements memo looks exactly the same, but now works.

Could the original "©" have hidden codes that make it not work?  Yes, its length now is 2, but it was 5.  As simple as bad data sneaking in?

Zvoni

  • Hero Member
  • *****
  • Posts: 2968
Re: stringreplace for the copyright symbol
« Reply #7 on: March 10, 2022, 02:53:44 pm »
Sorry, Zvoni, couldn't see the "=" sign you meant.

In your replacement-memo
Quote
the memo.lines must be added as
"@ = &copy"

© = ©
° = °
à = à
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018