Sign in to follow this  
Followers 0
Bot

StringInStr positions ?

6 posts in this topic

$text = "AutoIt has a StringInStr function. And StringInStr is to check if a string contains a given substring."

There are 2 occurrences of the text "StringInStr" in the $text but how can I return the positions of them in this ? Thanks.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#include <array.au3>
$text = "AutoIt has a StringInStr function. And StringInStr is to check if a string contains a given substring."
$aPos = _StringGetPos($text, "StringInStr", 1, 1)
_ArrayDisplay($aPos, "StringInStr Pos")

Func _StringGetPos($sText, $sFind, $nAll = 0, $nCase = 0)
    If $nAll Then
        Local $aSplit = StringSplit($sText, $sFind, 1)
        Local $avArray[$aSplit[0]]
        $avArray[0] = $aSplit[0]
        If $aSplit[0] > 0 Then 
            For $iCC = 1 To $aSplit[0] - 1
                $avArray[$iCC] = StringInStr($sText, $sFind, $nCase, $iCC)
            Next
            Return $avArray
        EndIf
        Return SetError(1, 0, 0)
    EndIf
    Local $avArray[2] = [1, StringInStr($sText, $sFind, $nCase)]
    If $avArray[1] <> 0 Then Return $avArray
    Return SetError(2, 0, 0)
EndFuncoÝ÷ ØGb´bëaÊ'y·­µêÚºÚ"µÍÚ[ÛYH   Ø^K]LÉÝÂÌÍÝ^H    ][ÝÐ]]Ò]ÈHÝ[Ò[Ý[Ý[Û[Ý[Ò[ÝÈÈÚXÚÈYHÝ[ÈÛÛZ[ÈHÚ][ÝXÝ[Ë][ÝÂÌÍØTÜÈHÔÝ[ÑÙ]ÜÊ    ÌÍÝ^ ][ÝÔÝ[Ò[Ý][ÝËKJBÐ^QÜ^J ÌÍØTÜË ][ÝÔÝ[Ò[ÝÜÉ][ÝÊB[ÈÔÝ[ÑÙ]ÜÊ  ÌÍÜÕ^   ÌÍÜÑ[   ÌÍÛ[H    ÌÍÛØÙHH
BRY ÌÍÛ[[BTÝ[ÔXÙJ ÌÍÜÕ^   ÌÍÜÑ[   ][ÝÉ][ÝÊBBSØØ[    ÌÍÛ^[YH^[YBSØØ[    ÌÍØ]^VÉÌÍÛ^[Y
ÌWBBIÌÍØ]^VÌHH ÌÍÛ^[YBRY    ÌÍÛ^[Y   ÝÈ[BBQÜ  ÌÍÚPÐÈHHÈ ÌÍÛ^[YBBBIÌÍØ]^VÉÌÍÚPÐ×HHÝ[Ò[Ý   ÌÍÜÕ^   ÌÍÜÑ[   ÌÍÛØÙK ÌÍÚPÐÊBBBS^BBT]    ÌÍØ]^BBQ[YBT]Ù]ÜK
BQ[YSØØ[  ÌÍØ]^VÌHHÌKÝ[Ò[Ý    ÌÍÜÕ^   ÌÍÜÑ[   ÌÍÛØÙJWBRY ÌÍØ]^VÌWH   ÉÝÈ[]    ÌÍØ]^BT]Ù]Ü
B[[

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.

Share this post


Link to post
Share on other sites

This is how I did it... Pretty much like Smokes second one... But his is better :)

Func _StringInStr ($sString, $sSubstring, $iCasesense = 0)
    $strings = StringReplace ($sString, $sSubstring, $sSubstring, 0, $iCasesense)
    $number = @extended
    Local $aArray[$number + 1]
    $aArray[0] = $number
    For $i = 1 to $number
        $aArray[$i] = StringInStr ($sString, $sSubstring, $iCasesense, $i)
    Next
    Return $aArray
EndFunc

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

This is how I did it... Pretty much like Smokes second one... But his is better :)

Func _StringInStr ($sString, $sSubstring, $iCasesense = 0)
    $strings = StringReplace ($sString, $sSubstring, $sSubstring, 0, $iCasesense)
    $number = @extended
    Local $aArray[$number + 1]
    $aArray[0] = $number
    For $i = 1 to $number
        $aArray[$i] = StringInStr ($sString, $sSubstring, $iCasesense, $i)
    Next
    Return $aArray
EndFunc
That will return an array regardless of whether the string to search even contains the pattern to find.

Edit:

Typo

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.

Share this post


Link to post
Share on other sites

That will return an array regardless of whether the string to search even contains the pattern to find.

Edit:

Typo

Woops... My Bad :) Hence why your's is better... :)

Share this post


Link to post
Share on other sites

Thank you very much for your help :). I'm really suck a newbie with AutoIT :)

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  
Followers 0