Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me sort these strings out..

Status
Not open for further replies.

simmo09

Programmer
Apr 22, 2009
82
0
0
GB
ok this is frazzling my head right now. ok lets get started, i have a listbox which its items are representing file paths. so for example, it may contain the following items:

"C:\Windows\Delphi\My Folder\Folder1"
"C:\Windows\Delphi\My Folder\Folder2"
"C:\Windows\Delphi\My Folder\Folder3"
"C:\Windows\Delphi\My Folder\Folder4"

the important thing to know here is that, after "C:\Windows\Delphi\" the strings could be anything.

what im trying to do is trim/delete/extract whichever each listbox item to get the values after "C:\Windows\Delphi\".

so i basically need to get these:

"C:\Windows\Delphi\My Folder\Folder1"
"C:\Windows\Delphi\My Folder\Folder2"
"C:\Windows\Delphi\My Folder\Folder3"
"C:\Windows\Delphi\My Folder\Folder4"

to look this:

"\My Folder\Folder1"
"\My Folder\Folder2"
"\My Folder\Folder3"
"\My Folder\Folder4"

ive tried Trim, TrimLeft, TrimRight, StringReplace and im baffled, im confusing myself and am getting annoyed. its fairly easy im sure, but im having slipping up somewhere.

Code:
for i:= 0 to MasterFileBufferList.Items.Count -1 do
begin
  oldPath:= ExtractFilePath(ExcludeTrailingPathDelimiter(MasterFileBufferList.Items.Strings[i]));
  oldPath:= TrimRight('\My Folder\Folder4');
end;

Please help!!!

Thanks :)
 
simply put, i need to cut out of the string:

"C:\Windows\" and leave the right hand side of the string in tact
 
ha, seem to of figured something out (was checking StrUtils.pas) and found this function ContaintsText.

if ContainsText()

seems to of worked, im open to other ideas though, but so far ive got it working using the forementioned function
 
If I can establish that the string starts at position 1, then all I would need to do is copy the rest of the path out of the original string.

Kind of untested, but hopefully it shows the idea.

Code:
instr := 'C:\Windows\Delphi\My Folder\Folder1';
search_str := 'C:\Windows\Delphi';
outstr := copy(instr, length(search_str) + 1, 255);

Measurement is not management.
 
While it's quite slow with longer strings, you could use StringReplace with the shorter strings and replace the unnecessary/unwanted text with ''.

Lee
 
The JCL contains all sorts of great shortcut functions including one call StrAfter which takes a string that you're looking for and the string being searched and returns the text after that string being searched. It eventually uses the base string functions so you could code it yourself - but why?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top