Split Function


Split function splits / separates a string expression into substrings using the specified delimiter and returns a one dimensional array that contains the separated substrings.

Syntax: Split(Expression,[Delimiter],[Count],[Compare])

Expression (Required) – String expression

Delimiter (Optional) – Parameter based on which String expression needs to be separated.
If not specified, Split operation would be performed using Space (“ “) as a delimiter by default.

Count (Optional) – The number of substrings to be returned from Split operation.
If not specified, default value is -1, which returns all substrings

Compare (Optional) – Specifies the type of comparison to be performed. It takes the below two values –
          0 (VbBinaryCompare) – Performs Binary comparison
          1 (VbTextCompare) – Performs Text comparison

If not specified, it performs Binary comparison by default.

Examples:

'Split a string using a specified delimiter
arrStrings = Split("QTP_Sreenu_Blog","_") 'Gives you the array containing 3 elements QTP, Sreenu and Blog
MsgBoxUBound(arrStrings) 'Output is 2: That is 3 elements
=======================================================================
'Split a string using another string as a delimiter
arrStrings = Split("QTP Sreenu Blog","e")
MsgBoxUBound(arrStrings) 'Output is 2
For i=0ToUBound(arrStrings)
    MsgBox arrStrings(i)
Next
'Output:
'arrStrings(0) = "QTP Sr"
'arrStrings(1) = ""
'arrStrings(2) = "nu Blog"
=======================================================================
'Split a string without using a delimiter
arrStrings = Split("QTP Sreenu Blog") 'Split operation would be performed using Space as delimiter
MsgBoxUBound(arrStrings) 'Output is 2
=======================================================================
'Split a string by specifying the count of substrings to be returned
arrStrings = Split("QTP_Sreenu_Blog","_",2) 'Splits the string into two parts
MsgBoxUBound(arrStrings) 'Output is 1
For i=0ToUBound(arrStrings)
    MsgBox arrStrings(i)
Next
'Output:
'arrStrings(0) = "QTP"
'arrStrings(1) = "Sreenu_Blog"
=======================================================================
'Split a string using Binary Compare
arrStrings = Split("QTP Sreenu Blog","E",3,0) 'Binary Compare, E does not match with e
MsgBoxUBound(arrStrings) 'Output is 0: That is only one element. So, no Split is done
=======================================================================
'Split a string using Text Compare
arrStrings = Split("QTP Sreenu Blog","E",3,1) 'Text Compare, E matches with e
MsgBoxUBound(arrStrings) 'Output is 2: That is only 3 elements.

No comments:

Post a Comment