Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 10-30-2017, 09:03 AM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 379
Default last slash

How to get string after last slash and before lash slash

some/anything/done

result 1 should be done

result 2
should be some/anything

Last edited by robert; 10-30-2017 at 06:07 PM.
Reply With Quote
  #2  
Old 10-30-2017, 01:11 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,218
Default

ExtractFileName / ExtractFileDir
Reply With Quote
  #3  
Old 10-30-2017, 01:23 PM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 379
Default

Quote:
Originally Posted by Norrit View Post
ExtractFileName / ExtractFileDir
I already Tried but this is a string not Opendialog path
Reply With Quote
  #4  
Old 10-30-2017, 02:18 PM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 379
Default

this is like this

Edit1.text:='some/anything/done';

I tried but not worked

ExtractFileName(Edit1.text);

Last edited by robert; 10-30-2017 at 06:07 PM.
Reply With Quote
  #5  
Old 10-30-2017, 07:47 PM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 173
Default

Try this
Code:
function TForm1.FindLastSlash(const Value: String): Integer;
var
  I: Integer;
begin
  Result := -1;
  I := Length(Value);
  While I > 0 do
  begin
    if Value[I] = '/' then
      Result := I
    else
      I := I - 1;
  end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  SlashPos: Integer;
  TheString: String;
  BeforeSlash: String;
  AfterSlash: String;
begin
  TheString := 'some/anything/done';
  SlashPos := FindLastSlash(TheString);
  if SlashPos <> -1 then
  begin
    BeforeSlash := copy(TheString, 1, SlashPos - 1);
    AfterSlash := copy(TheString, SlashPos + 1, Length(TheString));
  end
  else
    ShowMessage('No Slash Found in String');
end;
Reply With Quote
  #6  
Old 10-31-2017, 08:45 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,218
Default

ExtractFileDir and ExtractFileName work!!!

It doesn't use any file operating calls, it's simple string slicing in code
Only problem is that you didn't specify the slash, and there was your problem

ExtractFileDir and ExtractFileName work with backslash (\) and you now use forwardslash (/). But with some thinking you should've figured this out yourself! Stop copy/paste and then shout that it doesn't work. As long as you don't specify all your requirements in your question you cannot expect a copy/paste working answer. You're a developer, there should be some pro-active code understanding from your side!!!

As for your question, that can easily be fixed by calling ExtractFileDir(UnixPathToDosPath(yourString)), and the same for ExtractFileName

#edit
And yes, you can alter your original question, but next time post it completely before there are answers that aren't what you expect
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:11 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.