?
Solved

[DELPHI] Common string problem ...

Posted on 2003-03-28
9
Medium Priority
?
158 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
[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
  • 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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