• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

How to replace characters of this textfile?

Hi Folks!

I have textfiles, each filled with 400 characters > this looks something like this > AHFTGDHDGFHFGDHDGDGFH but then ofcourse up to 400. This results in 400 positions.
Now I want to replace some characters by means of their position because when this textfile is read into my stringgrid, some words must be formed. Now I have the positions of the characters to be replaced > for example > the characters at position 75 to 79 should be replaced with some other characters.
Suppose the textfile reads ABCDEFGHIJKLMNOPQRSTUVWXYZ.. and the positions to be replaced were 5,6,7, and 8 > the new characters would be PAUL then I want to be able to just have to define the positions and the new characters and after a button click the file should read: ABCDPAULIJKLMNOPQRSTUVWXYZ..
I also would like to be able to generate a 400 character textfile with random characters in it. If you need more info please let me know.

Regards,

Peter

 
0
PeterdeB
Asked:
PeterdeB
2 Solutions
 
pritaeasSoftware EngineerCommented:
Since you know the positions and length of the items to replace, you can use the Copy() function to break the stringat the correct places and join them back together again.

s := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
s := Copy(s, 1, 4) + 'PAUL' + Copy(s, 9, Length(s))
0
 
mikelittlewoodCommented:
To create a string of 400 random characters (captial letters A to Z)

var
  s: String;
  i, iLoop: Interger;
begin
  s := '';
  Randomize;
  For iLoop := 1 to 400 do
  begin
    i := 64 + Trunc(Random(25));
    s := s + Char(i);
  end;

I think something like this would create you a random string 400 capital characters long
0
 
pcsentinelCommented:
Random file generatror

var
li: integer;
  ls: string;
  lFile: TextFile;
begin

  randomize;
  for li:=0 to 399 do
        ls:=ls+Chr(65+(trunc(random(26))));
  AssignFile(lFile,'rand.txt');
  Rewrite(lFile);
  WriteLn(lFile,ls);
  CloseFile(lFile);
end;


to replace the data in the string you could use delete and insert



const
  lstart=5;
  lLength=6;
  lreplace='paul11';


      delete(ls,lstart,llength);
      insert(lreplace,ls,lstart);


regards

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mikelittlewoodCommented:
by the way

A = 65
Z = 90

oops and this should say this I think

i := 65 + Round(Random(26));
0
 
mikelittlewoodCommented:
Actually pcsentinel is correct

i := 65 + Trunc(Random(26));

my bad
0
 
kretzschmarCommented:
if you know the index of the char,
then you can replace characters simple like

s[TheIndexYouKnow] := 'X';  //whereas s is your 400 CharacterString

or in your sample above:
>Suppose the textfile reads ABCDEFGHIJKLMNOPQRSTUVWXYZ.. and the positions to be replaced >were 5,6,7, and 8 > the new characters would be PAUL

s := YourContent; //ABCDEFGHIJKLMNOPQRSTUVWXYZ..
s[5] := 'P';
s[6] := 'A';
s[7] := 'U';
s[8] := 'L';

meikl ;-)
0
 
PeterdeBAuthor Commented:
Hi Folks!

Tnx for responding all of you!

pcsentinel > thanks for the randomizing > works great!
meikl > thanks for the char replacing > works great!

So I'll split the points.

Regards Peter
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now