Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

How can I delete specific characters of a text variable ?

I'd like to delete the last four characters of a line
of text contained in a String variable named S:

procedure TForm1.CreerButtonClick(Sender: TObject);
var
  I: Integer;
//  Should S really be a String ?
  S, T, N: String;
  F: TextFile;
begin
//
//  N.B. : All the lines of DropMemo
//  are character strings of filenames
//  of various lengths :
//  thefile.ext
//  namefile.ext
//  ohfilemyfile.ext
//  file.ext
//  ...
//
//  For each line of text in DropMemo
for I := 0 to DropMemo.Lines.Count - 1 do
  begin
//  Put the current line of text in S
    S := DropMemo.Lines[I];
//  Concatenate the text in CheminEdit and S
//  to create the final character string T
    T := CheminEdit.Text + S;
//  Here I'd like to delete
//  the last four characters
//  of the character string S
>>  S := S - last4characters;  <<
//  so that I can add .bat at the end
//  to make the filename N
    N := S + '.bat';
    AssignFile(F, N);
    Rewrite(F);
//  BTW, can I create the file with a path
//  like c:\my1stfolder\my2ndfolder\
//  instead of no path ???
    Write(F, T);
    CloseFile(F);
  end;
end;

In general, I'd like to know how to
manipulate character strings easily...

Thanks a lot,

Basile ;) Absolute beginner in Delphi 6 ;)

P.S. : At www.madshi.net I found mad*,
a freeware VCL component collection
by Mathias Rauen which contains functions like
DeleteR => DeleteR('test123', 3)  ->  'test'
or ReplaceStr => ReplaceStr('Funnyny.', 'Funny', 'Fun')  ->  'Funny.'
but I'd really like to know if there isn't a simple way
to do that with the default Delphi 6 functions...
0
Bazile
Asked:
Bazile
  • 7
  • 4
  • 3
1 Solution
 
ziolkoCommented:
var s: string;

s:='hello world';
SetLength(s,Length(s)-4);
ShowMessage(s) // <- this will show: hello w

ziolko.
0
 
ziolkoCommented:
If You want only change file extension use: ChangeFileExt function
ziolko.
0
 
kretzschmarCommented:
dest := copy(source,1,length(source)-4);

whereas dest and source are both strings
(maybe -3 instead of -4 is needed)

just from head
not tested

meikl ;-)

0
Industry Leaders: 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!

 
kretzschmarCommented:
>If You want only change file extension use: ChangeFileExt function
yep
0
 
ziolkoCommented:
wow I'm impressed I was faster than kretzschmar :-))
p.s. Hi kretzschmar!:-)
ziolko.
0
 
BazileAuthor Commented:
Guess I have two valid answers
from ziolko and meikl.

How can I give you 20 points each ?

Do you still recommend using String
variables for manipulating text
and how can I simply reproduce
mad*'s ReplaceStr or other advanced
functions ?
0
 
ziolkoCommented:
> BTW, can I create the file with a path
> like c:\my1stfolder\my2ndfolder\
> instead of no path ???

in Your case N:='c:\my1stfolder\my2ndfolder\'+S+'.bat'
AssignFile(F,N);
or
N:='c:\my1stfolder\my2ndfolder\'+ChangeFileExt(S,'bat')
AssignFile(F,N);
without any previuse SetLength()

ziolko.


   
0
 
BazileAuthor Commented:
Oh boy !

Ziolko is FAST today!!!
0
 
ziolkoCommented:
Bazile You can posiibly reduce ammount of string variables but yes string is right type if You want to "play" with text.
ziolko.
p.s. if You want split points go to CommunitySupport and post question there (assign 0 points ask for splitting points, poit out who should recive points and how many, dont forget to place question ID in Your Q)
ziolko.
0
 
kretzschmarCommented:
give it ziolko,
because he was correct
and faster than i :-))
0
 
ziolkoCommented:
...besides kretzschmar has over half a million points :-)))
but seriouly Bazile is the man who gives points.
ziolko.
0
 
BazileAuthor Commented:
OK so, finally, after careful examination
of the photo finish, ZIOLKO WINS !!!

Besides odds were 43/1 in favour of meikl...
so I should be a millionaire by now :)))

Ziolko gets the 40 points + 10 bonus points
for speed and perseverance...

That said, I'll award him only Monday the 10th to keep
this thread in the "Questions Awaiting Answers " section
until then.

So if you have anything to add relevent
to character strings manipulation,
please do it NOW ! (But no more cookies :)

Basile ;) Absolute beginner in Delphi 6 ;)
0
 
kretzschmarCommented:
>so I should be a millionaire by now :)))

i wished i would be a millionaire,
but these points are just points ;-)))
0
 
ziolkoCommented:
meilkl even without them you're at top spot :-))
ziolko.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now