?
Solved

[DELPHI] Common string problem ...

Posted on 2003-03-28
9
Medium Priority
?
159 Views
Last Modified: 2010-04-04
Hi all,

My app reads a string from the registry (the path of my apps folder) and it returns the string like so "C:\Program Files\Proxy -uninstall" with the quotes. Now my question is how do I remove the "" and -uninstall part from the string so I would just have the actual path left C:\Program Files\Proxy
Thank You.

Sincerely, Tom Lenen
0
Comment
Question by:TomLenen
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 7

Expert Comment

by:billious
ID: 8229278
stringname := copy(stringname,2,length(stringname)-12);

since you want to copy from the second character (2)
and the number of characters you want is 12 fewer than the number of characters currently in the string.

You can also approach this problem using the delete procedure and pos function if you like (see the help!)

...Bill


0
 
LVL 1

Expert Comment

by:nafeelm
ID: 8229962

Save the apps folder path into the registry as 'C:\Program Files\Proxy\ -uninstall' instead of 'C:\Program Files\Proxy -uninstall'

then the below mentioned code would work

procedure TForm1.Button1Click(Sender: TObject);
var s: string;
begin
  s:='"C:\Program Files\Proxy\ -uninstall"';
  showmessage(copy(extractfilepath(s),2, length(s)));
end;

0
 

Author Comment

by:TomLenen
ID: 8231112
billious, that method would only work if that is the exact path since you're guessing its 12 characters long.

nafeelm, that method doesn't work ...

anyone else ...please?
0
Independent Software Vendors: 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!

 

Expert Comment

by:-TM-
ID: 8231167
use
Path := StringReplace(Path, '"', '', [rfReplaceAll]);
Path := StringReplace(Path, '-uninstall', '', [rfReplaceAll]);

That will replace all the " with nothing, basicly just removes it, same with -uninstall

needs SysUtils in uses clause

Hope it helps

-TM-
0
 

Author Comment

by:TomLenen
ID: 8232333
The path is returned in an array[0..255] of char so I can't use ReplaceString since it only allows strings. Any ideas?
0
 
LVL 7

Expert Comment

by:billious
ID: 8232801
Well, I'm confused. Are you working on a string or a char-array? You said "string" four times in your question, and now you reveal you want to operate on an array of char.

You also say you want to remove -uninstall" which is a 12-character string. It would appear obvious then that if that string is not -uninstall" then you should change the code to

stringname := copy(stringname,2,length(stringname)-length(string_you_want_to_remove_from_the_end));

of course, that assumes that you want to remove just the leading "

If you are using an array, then

move(temp[1],temp[0],length(temp)-length(string_you_want_to_remove_from_the_end));

but of course, that's just moved the characters in your char-array. You'd possibly want to put a #0 at length(original)-succ(length(string_you_want_to_remove_from_the_end))

...Bill

 

0
 

Author Comment

by:TomLenen
ID: 8233537
Is there any way of using ReplaceString with an array of char?
0
 

Author Comment

by:TomLenen
ID: 8233883
nevermind, I got it:

eg. StrPCopy(CharArray,StringReplace(String(CharArry),OldPattern,NewPattern,[rfReplaceAll]));

Sincerely, Tom Lenen

Who should get the points now?
0
 
LVL 1

Accepted Solution

by:
nafeelm earned 200 total points
ID: 8237627

As of now you save the apps folder path in registry as
C:\Program Files\Proxy -uninstall ... right?

instead save it as
C:\Program Files\Proxy\ -uninstall (note the backslash being added after the word "Proxy")

Assuming "MyAppPath" is the StringKey storing your apps folder path


then try the below mentioned code .. this will surely work!


procedure TForm1.Button1Click(Sender: TObject);
var reg: TRegistry; AppPath: string;
begin
  AppPath:=Reg.ReadString('MyAppPath');
  showmessage(copy(extractfilepath(AppPath), 1, length(AppPath)));
end;

i have tried this at my end & it works just perfect!
let me know!
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!

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

612 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