Jump to content

StringinStr broken?


EndFunc
 Share

Recommended Posts

xD

StringInStr is SUPPOSED to return a number. It returns the index of the string that is found.

Hmm, am I using it wrong because I don't remember it doing that. What is it supposed do here?

$result = StringInStr("I am a String", "RING")
MsgBox(0, "Search result:", $result)

$location = StringInStr("How much wood could a woodchuck chuck is a woodchuck could chuck wood?", "wood", 0, 3) ; Find the 3rd occurance of "wood"

The number 10 would make no sense.

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

  • Moderators

It starts at the 10th character of the string, it makes perfect sense.

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

It starts at the 10th character of the string, it makes perfect sense.

I see, yeah I understand. Maybe I'm think of StringSplit.

Ok say Im trying to get the folder name in a path and wanting to get the name after the last "\" in the string.

That is what I was attempting to do. It was giving me a number but I believe I got them confused. Any suggestion on how to do this the most efficient way. I was also using a -1 to make it start on the right side which further confused me. lol

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

Dim $aPath_FileName = StringRegExp($sPath, (.+\\)(.+), 3)

This will return the path plus the '\' in the first element of the array and the 'filename.ext' in the second element, or, fail in case of strings like 'Notepad.exe', 'C:\Games\' etc..

Ok I will check it out. I wanted to retrieve the very last folder in the path and single that out and you wont know how long the directory is.

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

  • Moderators

Dim $aPath_FileName = StringRegExp($sPath, (.+\\)(.+), 3)

This will return the path plus the '\' in the first element of the array and the 'filename.ext' in the second element, or, fail in case of strings like 'Notepad.exe', 'C:\Games\' etc..

StringRegExpReplace($sPath, "(.+?[\\/]+)+(.+?\z)", "\2")

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

... I was also using a -1 to make it start on the right side which further confused me.

$sPath = "c:\1st\2nd\3rd\4th\5th\file.txt"
$last = StringInStr($sPath, "\", 0, -1)
$next2last = StringInStr($sPath, "\", 0, -1, ($last - 1))
$lastFolder = StringMid($sPath, ($next2last + 1), ($last - 1) - $next2last)
MsgBox(0, "StringMid", $lastFolder)

;or per Authenticity

$PathArray = StringSplit($sPath, "\")
MsgBox(0, "StringSplit", $PathArray[$PathArray[0] - 1])

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Since you want two pieces, you can use _PathSplit, or alternatively here's a quick path-split call:

$aSplitPath=StringRegExp($sPath,'([^\\\:]+)',3)

Given C:a folder\a file, it will return an array like this:

[0] = C

[1] = a folder

[2] = a file

Given C:\a\complete\path\filename.ext, it will return

[0] = C

[1] = a

[2] = complete

[3] = path

[4] = filename.ext

Ascen4nt

Link to comment
Share on other sites

Thanks all for the suggestions. I actually don't care about the file. There is no file I'm looking for. Just the very last folder/directory. C:\1\2\3\4\5\6\7. So in that, it would be 7, some cases I won't know what the last folder will be. I will play around with these and see what I come up with. :P

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...