Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   replace string with spaces? (https://www.delphipraxis.net/203158-replace-string-spaces.html)

direktor 19. Jan 2020 05:12

replace string with spaces?
 
Hello,

I have another problem, how to pass the Space in string that I want to replace?

For example I want to find string 'this is just a test' and replace it with 'this is not a test'. Problem is Delphi will find only first word 'this' - everything after space is ignored. How do I make it find full string not that it stops at space? Or I'm using Delphi version which is too low? Should I use Unicode string compare?

'this is something else' - delphi will stop here because 'this' is contained here and 'is something else' it treats like it doesn't even exist because of space between

'this is just a test' - but I want to change this line of text down below, the whole text including spaces

How to do? StringReplace doesn't work.

Thanks.

hhcm 19. Jan 2020 07:43

AW: replace string with spaces?
 
Try this

Uwe Raabe 19. Jan 2020 10:24

AW: replace string with spaces?
 
Please give us a small test program that shows the error. Also mention the Delphi version you are using.

direktor 19. Jan 2020 12:10

AW: replace string with spaces?
 
Uwe there is no error. Just doesn't work like it's suppose to. It can't replace text with spaces, only can replace the first word matched, anything after space is not replaced - as a result it replaces wrong string that begins with the same word but text after first space is different. I'm using Delphi 6, but I have Delphi XE8 also installed and can be used. Thanks.

direktor 19. Jan 2020 12:16

AW: replace string with spaces?
 
Try this:

tmpstr.loadfromfile('index.html);
StringReplace(tmpstr.Text,'Electronics online shop','Electronic offline brick shop',[rfReplaceAll]);

This code will result in replacing every string that begins with word Electronics, because it can't handle spaces. I want to replace only exact string 'Electronics online shop' with spaces included.

DieDolly 19. Jan 2020 12:17

AW: replace string with spaces?
 
Zitat:

This code will result in replacing every string that begins with word Electronics, because it can't handle spaces.
StringReplace can handle spaces.

direktor 19. Jan 2020 16:30

AW: replace string with spaces?
 
But not in Delphi 6. Maybe I should do this code in XE8.

DieDolly 19. Jan 2020 16:40

AW: replace string with spaces?
 
Many people will kill me now but: nobody should use old Delphi versions anymore.
Many people's excuse is "the executable file is smaller" but this is only a bad excuse.

Dalai 19. Jan 2020 16:51

AW: replace string with spaces?
 
Zitat:

Zitat von direktor (Beitrag 1455531)
But not in Delphi 6.

Yes, it does. I just tested it in Delphi 5:
Delphi-Quellcode:
program Project1;
{$APPTYPE CONSOLE}
uses
  sysutils,
  classes;

var
   tmpstr: TStringList;
   s: string;

begin
  tmpstr:= TStringList.Create;
  try
    tmpstr.LoadFromFile('c:\boot.ini');
    s:= StringReplace(tmpstr.Text, 'boot loader', 'boot blubber', [rfReplaceAll]);
    WriteLn(s);
  finally
    tmpstr.Free;
  end;
  ReadLn;
end.
works just fine in Windows 2000 and replaces 'boot loader' with 'boot blubber'. If you really use the code from post #5 above, it's no wonder it doesn't work because you don't assign the result of the StringReplace function to anything.

Regards
Dalai

direktor 19. Jan 2020 17:43

AW: replace string with spaces?
 
But it still doesn't work for me. Not in Delphi 6 not in XE8. Here are files for both Delphi versions 6 and XE8:

https://www.mediafire.com/file/p8sli...elphi.zip/file

Please check whats wrong. Thanks.


Alle Zeitangaben in WEZ +1. Es ist jetzt 03:08 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz