?
Solved

How can I delete specific characters of a text variable ?

Posted on 2003-03-07
14
Medium Priority
?
262 Views
Last Modified: 2010-05-18
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
Comment
Question by:Bazile
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 3
14 Comments
 
LVL 21

Accepted Solution

by:
ziolko earned 200 total points
ID: 8087765
var s: string;

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

ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 8087770
If You want only change file extension use: ChangeFileExt function
ziolko.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8087779
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
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!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 8087782
>If You want only change file extension use: ChangeFileExt function
yep
0
 
LVL 21

Expert Comment

by:ziolko
ID: 8087811
wow I'm impressed I was faster than kretzschmar :-))
p.s. Hi kretzschmar!:-)
ziolko.
0
 

Author Comment

by:Bazile
ID: 8087845
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
 
LVL 21

Expert Comment

by:ziolko
ID: 8087846
> 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
 

Author Comment

by:Bazile
ID: 8087870
Oh boy !

Ziolko is FAST today!!!
0
 
LVL 21

Expert Comment

by:ziolko
ID: 8087917
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8087940
give it ziolko,
because he was correct
and faster than i :-))
0
 
LVL 21

Expert Comment

by:ziolko
ID: 8088004
...besides kretzschmar has over half a million points :-)))
but seriouly Bazile is the man who gives points.
ziolko.
0
 

Author Comment

by:Bazile
ID: 8089134
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8102889
>so I should be a millionaire by now :)))

i wished i would be a millionaire,
but these points are just points ;-)))
0
 
LVL 21

Expert Comment

by:ziolko
ID: 8102950
meilkl even without them you're at top spot :-))
ziolko.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question