how do i get the last 3 letters of a string

Go down

how do i get the last 3 letters of a string

· #1

how do i get the last 3 letters of a string

Re: how do i get the last 3 letters of a string

· #2
pascal
Last3 := Copy(S, Length(S) - 2, 3);

or with the helper from StrUtils: RightStr(S, 3).

Re: how do i get the last 3 letters of a string

· #3

ok that works for photo.png but for picture.jpeg it gives peg

Re: how do i get the last 3 letters of a string

· #4

It gives peg because the last three letters of picture.jpeg are peg. The code is right. I suspect the question isn't.

What are you actually trying to find out?

Re: how do i get the last 3 letters of a string

· #5

the letters after the dot

Re: how do i get the last 3 letters of a string

· #6

That's the file extension, and it has its own function:

pascal
Ext := ExtractFileExt('picture.jpeg');   // '.jpeg', the dot included

It works however long the extension is. Now, one more time: what do you want the extension for?

Re: how do i get the last 3 letters of a string

· #7

to know if the file is a picture

Re: how do i get the last 3 letters of a string

· #8

There it is. Three questions deep. This has a name: you asked about your solution (last three letters) when you needed help with your problem (is it a picture).

pascal
function IsPicture(const FileName: string): Boolean;
begin
  case LowerCase(ExtractFileExt(FileName)) of
    '.png', '.jpg', '.jpeg', '.gif', '.bmp': Result := True;
  else
    Result := False;
  end;
end;

LowerCase matters, because cameras love to write .JPG.

Next time, start with the last sentence. "I want to know if a file is a picture." You'd have had this in one reply.

Read the error message. Then read it again.

Re: how do i get the last 3 letters of a string

· #9

ok

how do i get the first 4 letters of a string

Re: how do i get the last 3 letters of a string

· #10

What for.

Forum rules 1 to 10 apply. Yes, to you.

Re: how do i get the last 3 letters of a string

· #11

...to know if a link starts with http

Re: how do i get the last 3 letters of a string

· #12

S.StartsWith('http'). We're getting faster at this.

Forum rules 1 to 10 apply. Yes, to you.

Go up

Sign in to reply