Jump to content

StringTrimRight giving weird results


Recommended Posts

I have a line of text and when I try to trim the right it gives weird results.

Example

$sPath = "C:\scripts\autoit\mypath\morescripts\file.exe"

When I do this

$nPath = StringTrimRight($sPath,StringInStr($sPath,"\",0, -1))

It returns something weird like C:\scripts\autoit\mypath\mores

Instead of C:\scripts\autoit\mypath\morescripts\

What am I doing wrong cause my brain must be fried right now.

Edited by EndFunc
EndFuncAutoIt is the shiznit. I love it.
Link to comment
Share on other sites

Your StringInStr is returning position of the rightmost "\" starting from the left side which is like the 37th character. So 37 characters are being trimmed from the right side of the string.

Add:

Something like this could work...

;
;
$nPath = StringTrimRight($sPath,StringLen($sPath) - StringInStr($sPath,"\",0, -1))
 ;
;
Edited by MrMitchell
Link to comment
Share on other sites

Reread the definition of StringInStr output, then the definition of StringTrimRight second parameter.

Then realize that the result of the former can't be (in your case) used directly by the latter!

Edit: I was diverted and overtaken!

Simpler:

$nPath = StringLeft($sPath, StringInStr($sPath,"\",0, -1))

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

StringLeft will give the answer you are looking for.

$sPath = "C:\scripts\autoit\mypath\morescripts\file.exe"
$nPath = StringLeft($sPath,StringInStr($sPath,"\",0, -1))
Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

  • Moderators

MrMitchell pointed out what you needed with StringTrimRight ... You could have used StringLeft to achieve the same results the way you originally had it though:

$nPath = StringLeft($sPath, StringInStr($sPath,"\",0, -1))

Edit:

As Bowmore showed :mellow:

Edit2:

And as jchd presented in his edit :P:party:

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Your StringInStr is returning position of the rightmost "\" starting from the left side which is like the 37th character. So 37 characters are being trimmed from the right side of the string.

Add:

Something like this could work...

;
;
$nPath = StringTrimRight($sPath,StringLen($sPath) - StringInStr($sPath,"\",0, -1))
 ;
;

Ahh,doh.. :mellow: That makes sense. I guess I was confusing myself.

Thanks for the help and setting me straight.

EndFuncAutoIt is the shiznit. I love it.
Link to comment
Share on other sites

MrMitchell pointed out what you needed with StringTrimRight ... You could have used StringLeft to achieve the same results the way you originally had it though:

$nPath = StringLeft($sPath, StringInStr($sPath,"\",0, -1))

Edit:

As Bowmore showed :mellow:

Thanks SmOke_N, that is actually what I was trying to do because I had done it before but couldn't understand why it wasn't working. Wrong function. lol I should try and find my old scripts as reference.
EndFuncAutoIt is the shiznit. I love it.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...