Jump to content
Sign in to follow this  

String function to count all the space positions

Recommended Posts


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.

Share this post

Link to post
Share on other sites

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', ' ')


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
    $array[0] = UBound($array) - 1
    Return $array

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

Share this post

Link to post
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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.