Author Topic: Possible bug in Function StrTrimLeft()  (Read 27225 times)

ajax

  • Junior Member
  • **
  • Posts: 25
    • View Profile
Possible bug in Function StrTrimLeft()
« on: July 08, 2018, 20:34:07 »
It looks like there may be a bug in function (StrTrimLeft) which I'm using to separate the file name extension from the remaining part of the file name.  For whatever it is worth the files I'm working with are images and a typical extension is .jpg.  I'm using the StrRFind function to locate the right most period (".") in the filename.  Then StrSub to obtain the portion of the filename that includes everything up to the period.  This all works as expected.

However, when I then use StrTrimLeft to obtain just the file name extension I get different results depending on how the filename is composed.  In my case, whenever the filename has only one period (".") that separates the extension from the rest of the filename I get the expected result.  For example, if the filename is "PartA.jpg" I get the extension as ".jpg".  However, if the file name where "PartA.PartB.jpg" I get the extension as "jpg".  In that, an extra period (".") character is being trimmed.  The same, incorrect result, seems to occur whenever the filename contains more than one period (".").

Following are a couple of lines of code that experience the problem -

@var $linkname = StrSub($filename, 0, StrRFind($filename, "."));
@var $linkext = StrTrimLeft($filename, $linkname);

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4409
    • View Profile
    • Multi Commander
Re: Possible bug in Function StrTrimLeft()
« Reply #1 on: July 08, 2018, 21:15:19 »
StrTrimLeft do not trim complete strings. It trim characters.. It is primary used are to remove spaces and tab and such from strings.
So you do not specify complete "words" to remove. But all the characters that should be removed, like spaces , tab characters and so on.

So you are trying to get the filename part without the extension.. and you removed the filename part with the extensions, so you are left with the extension ?

I think you can you PathGetFileExtPart(..) instead..

Code: [Select]
$newname = PathGetFileExtPart( $filename );      // ".mp3"
$newname = PathGetFileExtPart( $filename , 1 ); // strip the "." from the extension eg  "mp3"

If you really want to use string function.. Can't you just do
Code: [Select]
@var $linkext= StrSub($filename, StrRFind($filename, ".") );

ajax

  • Junior Member
  • **
  • Posts: 25
    • View Profile
Re: Possible bug in Function StrTrimLeft()
« Reply #2 on: July 09, 2018, 15:51:20 »
I was able to revise my script so that it works fine without using StrTrimLeft.  Your clarification of its' purpose also makes it clear that it was not a good choice.  It just happened to work under some circumstances.

THANKS!  Sorry to trouble you.