Jump to content

String Explode


weaponx
 Share

Recommended Posts

New version:

#include <Array.au3>

$text = "one##two##three##four##five##six##seven##eight"
$array1 = StringExplode($text, "##", 0)
_ArrayDisplay($array1)

;DISPLAYED
;[0] = 8
;[1] = one
;[2] = two
;[3] = three
;[4] = four
;[5] = five
;[6] = six
;[7] = seven
;[8] = eight

$array2 = StringExplode($text, "##", 4)
_ArrayDisplay($array2)

;DISPLAYED
;[0] = 5
;[1] = one
;[2] = two
;[3] = three
;[4] = four
;[5] = five##six##seven##eight

$array3 = StringExplode($text, "##", -3)
_ArrayDisplay($array3)

;DISPLAYED
;[0] = 5
;[1] = one
;[2] = two
;[3] = three
;[4] = four
;[5] = five

#cs ----------------------------------------------------------------------------
AutoIt Version: 3.2.12.1
Author: WeaponX

Script Function: PHP Explode v2

Parameters:
    ssString = String to be split
    ssDelimiter = Delimiter to split on (split is performed on entire string, not individual characters)
    ssLimit =
        0: Split on every instance of the delimiter
        >0: Split until limit, last element will contain remaining portion of the string
        <0: Split on every instance, removing limit count from end of the array
#ce ----------------------------------------------------------------------------
Func StringExplode($sString, $sDelimiter, $sLimit = 0)
    If $sLimit > 0 Then
        ;Replace delimiter with NULL character using given limit
        $sString = StringReplace($sString,$sDelimiter,Chr(0),$sLimit)
        
        ;Split on NULL character, this will leave the remainder in the last element
        $sDelimiter = Chr(0)
    ElseIf $sLimit < 0 Then
        ;Find delimiter occurence from right-to-left
        $iIndex = StringInStr($sString,$sDelimiter,0,$sLimit)
        
        If $iIndex Then
            ;Split on left side of string only
            $sString = StringLeft($sString,$iIndex-1)
        EndIf
    EndIf
    
    Return StringSplit($sString,$sDelimiter,1)
EndFunc
Edited by weaponx
Link to comment
Share on other sites

This is exactly what i asked for with this feature request.

I knew it would be possible to do in autoit, but i figured it would be a useful built in parameter for StringSplit, and since it would be optional, it wouldn't break any scripts.

Link to comment
Share on other sites

This is exactly what i asked for with this feature request.

I knew it would be possible to do in autoit, but i figured it would be a useful built in parameter for StringSplit, and since it would be optional, it wouldn't break any scripts.

I actually saw your request and thought I would give it a try. I wish we had more functions like PHP and dream of the day we get associative arrays.

Edited by weaponx
Link to comment
Share on other sites

  • 5 months later...

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