Warcraft III resources & community, 2003–2006 · archived

GetDividedStringByIndex

not rated
Submitted by AltSubmitterText Parsing0 downloads
Get the (index)th part of string (S), which is divided by (divider).
ex:
GetDividedStringByIndex( " abc , def , ghi " , "," , 1) => " abc "
GetDividedStringByIndex( "abc,def,ghi" , ", " , 1) => "abc,def,ghi"
GetDividedStringByIndex( "abc,def,ghi" , ", " , 2) => ""
GetDividedStringByIndex( " abc , def , ghi " , ", " , 2) => "def "
GetDividedStringByIndex( " abc , def , ghi " , ", " , 3) => "ghi "
GetDividedStringByIndex( ",abc,def,ghi" , "," , 3) => "def "

Source

function GetDividedStringByIndex takes string S, string divider, integer index returns string
  local string match
  local integer len = strlen(divider)
  local integer start = 1
  local integer end = 0
  local integer count = 0
  local integer i = 1

    if S == null then
        return null
    endif
    if (S == "") or (len == 0) or (index<=0) then
        return ""
    endif
    loop
        set match = SubString(S,i-1,i+len-1)
        if ( match == divider ) or ( match == "" ) then
            set count = count + 1
            if ( count == index-1 ) then
                set start = i+len
            endif
            if ( count == index ) then
                set end = i-1
                exitwhen true
            endif
        endif
        exitwhen match == ""
    set i = i + 1
    endloop
    return SubString(S,start-1,end)
endfunction

Comments

0

No comments.