Difference between revisions of "StringSplit"

From AutoIt Wiki
Jump to navigation Jump to search
(Created page with "==Description== Splits up a string into substrings depending on the given delimiters. <pre> StringSplit ( "string", "delimiters" [, flag = 0] ) </pre> ==Parameters== {| bord...")
 
 
Line 1: Line 1:
==Description==
+
<!-- Autogenerated Wiki Page. -->
Splits up a string into substrings depending on the given delimiters.
+
<!-- Not meant to be user editable. -->
 +
<!-- If there is an error here then that means that there is an error in the helpfile. -->
 +
<!-- Report it to Trac with category 'Documentation'. -->
 +
<!-- See this post for more info: http://www.autoitscript.com/forum/topic/153680-convert-helpfile-to-wiki-text/-->
 +
<font face='Segoe UI, Courier New, Lucida Grande, Verdana, Helvetica, sans-serif'>
  
<pre>
+
<font size='3' weight='normal'>Splits up a string into substrings depending on the given delimiters.</font>
StringSplit ( "string", "delimiters" [, flag = 0] )
 
</pre>
 
  
==Parameters==
+
<font face='Courier New'><div id='Syntax' style='background-color:#FFFFAA;'>
{| border='1' class='wikitable'
+
 
| string || The string to evaluate.
+
 
 +
:StringSplit ( "string", "delimiters" [, flag = 0] )
 +
 
 +
 
 +
</div></font>
 +
 
 +
 
 +
 
 +
===<font size=4><div id='Section' style='color:#DB7100;'>Parameters</div></font>===
 +
{| class='wikitable'
 
|-
 
|-
| delimiters || One or more characters to use as delimiters (case sensitive).
+
| string || The string to evaluate.
 
|-
 
|-
| flag || [optional] Changes how the string split works, add multiple flag values together if required:
+
| delimiters || One or more characters to use as delimiters (case sensitive).
 
|-
 
|-
| || $STR_CHRSPLIT (0) = each character in the delimiter string will mark where to split the string (default)
+
| flag || '''[optional]''' Changes how the string split works, add multiple flag values together if required:
 
|-
 
|-
| || $STR_ENTIRESPLIT (1) = entire delimiter string is needed to mark the split
+
| || $STR_CHRSPLIT (0) = each character in the delimiter string will mark where to split the string (default)
 
|-
 
|-
| || $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).
+
| || $STR_ENTIRESPLIT (1) = entire delimiter string is needed to mark the split
 
|-
 
|-
| || Constants are defined in Constants.au3
+
| || $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==
+
 
 +
===<font size=4><div id='Section' style='color:#DB7100;'>Return Value</div></font>===
 
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.
 
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]).
 
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==
+
 
 +
 
 +
===<font size=4><div id='Section' style='color:#DB7100;'>Remarks</div></font>===
 
If you use an empty string "" for the delimiters, each character will be returned as an element.
 
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.
 
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.
+
[[StringSplit]] is very useful as an alternative to [[StringInStr]] 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).
 
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).
Line 39: Line 55:
 
To use the values specified above you must #include <StringConstants.au3> in your script.
 
To use the values specified above you must #include <StringConstants.au3> in your script.
  
==Related==
+
 
 +
 
 +
===<font size=4><div id='Section' style='color:#DB7100;'>Related</div></font>===
 
[[StringInStr]], [[StringLeft]], [[StringLen]], [[StringLower]], [[StringMid]], [[StringRight]], [[StringTrimLeft]], [[StringTrimRight]], [[StringUpper]]
 
[[StringInStr]], [[StringLeft]], [[StringLen]], [[StringLower]], [[StringMid]], [[StringRight]], [[StringTrimLeft]], [[StringTrimRight]], [[StringUpper]]
  
==Example==
+
 
 +
 
 +
===<font size=4><div id='Section' style='color:#DB7100;'>Example</div></font>===
 
<syntaxhighlight lang='autoit'>
 
<syntaxhighlight lang='autoit'>
 
#include <MsgBoxConstants.au3>
 
#include <MsgBoxConstants.au3>
Line 63: Line 83:
 
Next
 
Next
 
EndFunc  ;==>Example
 
EndFunc  ;==>Example
</syntaxhighlight>
+
</syntaxhighlight><syntaxhighlight lang='autoit'>
 
 
<syntaxhighlight lang='autoit'>
 
 
#include <StringConstants.au3>
 
#include <StringConstants.au3>
 
#include <MsgBoxConstants.au3>
 
#include <MsgBoxConstants.au3>
Line 87: Line 105:
 
Next
 
Next
 
EndFunc  ;==>Example
 
EndFunc  ;==>Example
</syntaxhighlight>
+
</syntaxhighlight><syntaxhighlight lang='autoit'>
 
 
<syntaxhighlight lang='autoit'>
 
 
#include <StringConstants.au3>
 
#include <StringConstants.au3>
 
#include <MsgBoxConstants.au3>
 
#include <MsgBoxConstants.au3>
Line 110: Line 126:
 
EndFunc  ;==>Example
 
EndFunc  ;==>Example
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
</font>
 
__NOTOC__
 
__NOTOC__
 +
__NOEDITSECTION__

Latest revision as of 16:14, 23 August 2013

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