Jump to content

Recommended Posts

Posted

Hello Guys,

I know this might be an easy one, I am trying to find a string function that will return me an array with all the positions of spaces in that string.

I know such functions exist in scripting languages like PHP or perl but I cannot seem to find it in Autoit.

If anyone could send me on a lead it would be appreciated.

Thank you.

Posted (edited)

There's probably a better, "proper" way, but this works.

;  _GetCharPos(string, character)
;  Returns an array of positions
;  $array[0] = total returns

#include <array.au3>  ; just for _ArrayDisplay()

$somevar = _GetCharPos('this is some string with a bunch of spaces', ' ')

_ArrayDisplay($somevar)

Func _GetCharPos($string, $search)
    Local $array[1], $i = 1, $ret
    While 1
        $ret = StringInStr($string, $search, 0, $i)
        If $ret = 0 Then ExitLoop
        ReDim $array[UBound($array) + 1]
        $array[$i] = $ret
        $i += 1
    WEnd
    $array[0] = UBound($array) - 1
    Return $array
EndFunc

edit - Made it return an array and be more UDF-ish for finding diff character types, just add in your own error checking.

Edited by xcal

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
  • Recently Browsing   0 members

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