StringSplit

From AutoIt Wiki
Revision as of 08:31, 18 August 2013 by Jaberwocky6669 (talk | contribs) (Created page with "==Description== Splits up a string into substrings depending on the given delimiters. <pre> StringSplit ( "string", "delimiters" [, flag = 0] ) </pre> ==Parameters== {| bord...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Description

Splits up a string into substrings depending on the given delimiters.

StringSplit ( "string", "delimiters" [, flag = 0] )

Parameters

string The string to evaluate.
delimiters One or more characters to use as delimiters (case sensitive).
flag [optional] Changes how the string split works, add multiple flag values together if required:
$STR_CHRSPLIT (0) = each character in the delimiter string will mark where to split the string (default)
$STR_ENTIRESPLIT (1) = entire delimiter string is needed to mark the split
$STR_NOCOUNT (2) = disable the return count in the first element - effectively makes the array 0-based (must use UBound() to get the size of the array in this case).
Constants are defined in Constants.au3

Return Value

Returns an array, by default the first element ($aArray[0]) contains the number of strings returned, the remaining elements ($aArray[1], $aArray[2], etc.) contain the delimited strings. If the flag parameter is set to $STR_NOCOUNT (2) then the count is not returned in the first element.

If no delimiters were found then @error is set to 1, count is equal to 1 ($aArray[0]) and the full string is returned ($aArray[1]).

Remarks

If you use an empty string "" for the delimiters, each character will be returned as an element.

If the delimiter you wish to use is a substring instead of individual single characters, see the example below.

StringSplit is very useful as an alternative to StringSplit as well as a means to populate an array.

Note that if you use the macro @CRLF you are referring to a 2 character string, so this will generate extra blanks lines. Therefore it is recommended to set the flag parameter to $STR_ENTIRESPLIT (1).

To use the values specified above you must #include <StringConstants.au3> in your script.

Related

StringInStr, StringLeft, StringLen, StringLower, StringMid, StringRight, StringTrimLeft, StringTrimRight, StringUpper

Example

#include <MsgBoxConstants.au3>

Example()

Func Example()
	Local $aDays = StringSplit("Mon,Tues,Wed,Thur,Fri,Sat,Sun", ",") ; Split the string of days using the delimeter "," and the default flag value.
	#cs
		The array returned will contain the following values:
		$aDays[1] = "Mon"
		$aDays[2] = "Tues"
		$aDays[3] = "Wed"
		...
		$aDays[7] = "Sun"
	#ce

	For $i = 1 To $aDays[0] ; Loop through the array returned by StringSplit to display the individual values.
		MsgBox($MB_SYSTEMMODAL, "", "$aDays[" & $i & "] - " & $aDays[$i])
	Next
EndFunc   ;==>Example
#include <StringConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
	Local $sText = "This\nline\ncontains\nC-style breaks." ; Define a variable with a string of text.
	Local $aArray = StringSplit($sText, '\n', $STR_ENTIRESPLIT) ; Pass the variable to StringSplit and using the delimeter "\n".
	; Note that flag paramter is set to $STR_ENTIRESPLIT (1) as it would split at \ or n otherwise.
	#cs
		The array returned will contain the following values:
		$aArray[1] = "This"
		$aArray[2] = "line"
		...
		$aArray[4] = "C-style breaks."
	#ce

	For $i = 1 To $aArray[0] ; Loop through the array returned by StringSplit to display the individual values.
		MsgBox($MB_SYSTEMMODAL, "", "$aArray[" & $i & "] - " & $aArray[$i])
	Next
EndFunc   ;==>Example
#include <StringConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
	Local $sText = "This\nline\ncontains\nC-style breaks." ; Define a variable with a string of text.

	; Pass the variable to StringSplit and using the delimeter "\n".
	; Note that flag paramter is set to $STR_ENTIRESPLIT (1) as it would split at \ or n otherwise.
	MsgBox($MB_SYSTEMMODAL, "", StringSplit($sText, '\n', $STR_ENTIRESPLIT)[2]) ; Directly access the array index by using array access on expression.
	#cs
		An internal temporary array is used to return a string that may contain one of the following values:
		$aArray[1] = "This"
		$aArray[2] = "line"
		...
		$aArray[4] = "C-style breaks."
	#ce
EndFunc   ;==>Example