String concatenation
As per Documentation - Language Reference - Operators:
&
Concatenates/joins two strings.
&=
Concatenation assignment.
Example:
Global $g_sText = "Long " & "string " & "here." & @CRLF
$g_sText &= "More text." & @CRLF
ConsoleWrite($g_sText)
Multi line statements
As per Documentation - Language Reference - Comments (emphasis added, as it causes mentioned "unterminated string" error):
Although only one statement per line is allowed, a long statement can span multiple lines if an underscore "_
" preceded by a blank is placed at the end of a "broken" line. String definition cannot be split in several lines, concatenation need to be used.
Example:
Global Const $g_sText = "Long " & _
"string " & _
"here." & _
@CRLF & _
"More text." & _
@CRLF
ConsoleWrite($g_sText)
Double-quotes
As per Documentation - FAQ - Double quotes:
If you want to use double-quotes inside a string then you must "double them up". So for every one quote you want you should use two. ...
or use single quotes instead ...
Examples available from source.
Defaults and limits
As per Documentation - Appendix - Limits/defaults:
4095
Maximum size for a line of script.
2,147,483,647
Maximum string length.
As per Documentation - Language Reference - Datatypes - Strings:
All AutoIt strings use UTF-16 (in fact and more precisely UCS-2) encoding.
As per Documentation - Intro - Unicode Support:
There are a few parts of AutoIt that don't yet have full Unicode support. These are:
Send and ControlSend - Instead, Use ControlSetText or the Clipboard functions.
Console operations are converted to ANSI.
Alternatives
Alternatives to hard coding include ClipGet()
and FileRead()
.
Text from clipboard
Example (select and copy text CTRL + C first):
Global Const $g_sText = ClipGet()
ConsoleWrite($g_sText & @CRLF)
Text from file
Example (create C:\my_long_string.txt
first):
#include <FileConstants.au3>
Global Const $g_sFile = 'C:\my_long_string.txt'
Global Const $g_sText = _TextFromFile($g_sFile)
ConsoleWrite($g_sText & @CRLF)
Func _TextFromFile(Const $sFile)
Local $hFile = FileOpen($sFile, $FO_READ + $FO_UTF8_NOBOM)
Local Const $sData = FileRead($hFile)
FileClose($hFile)
Return $sData
EndFunc
Split string
Alternatives to hard coded manual string splitting include StringSplit()
, _StringExplode()
(related) and StringMid()
.
Structural
StringSplit()
splits a string into array of:
- individual characters (on empty delimiter),
- words (on space
delimiter) or
- lines (on
@CRLF
, @LF
or @CR
delimiter).
Equal length
StringMid()
returns part of a string. Can be used to split into parts of equal length. Example (no error checking, select and copy text CTRL + C first):
#include <Array.au3>
Global Const $g_iSize = 10
Global Const $g_sText = ClipGet()
Global Const $g_aArray = _StringSplitEqual($g_sText, $g_iSize)
_ArrayDisplay($g_aArray)
Func _StringSplitEqual(Const $sText, Const $iSize = 1)
Local Const $iLength = StringLen($sText)
Local Const $iParts = Ceiling($iLength / $iSize)
Local Const $iRest = -1; $iLength - ($iSize * Floor($iLength / $iSize))
Local $iStart = 0
Local $iCount = 0
Local $aArray[$iParts]
For $i1 = 0 To $iParts - 1
$iStart = ($i1 * $iSize) + 1
$iCount = ($i1 < $iParts - 1) ? $iSize : ($iRest ? $iRest : $iSize)
$aArray[$i1] = StringMid($sText, $iStart, $iCount)
Next
Return $aArray
EndFunc
Join string
As per documentation:
_ArrayToString
Places the elements of a 1D or 2D array into a single string, separated by the specified delimiters
Example (add _StringSplitEqual()
and select and copy text CTRL + C first):
#include <Array.au3>
Global Const $g_iSize = 10
Global Const $g_sStart = '$sText = "'
Global Const $g_sEnd = '"' & @CRLF
Global Const $g_sDelimiter = '" _' & @CRLF & ' & "'
Global Const $g_sText = StringReplace(ClipGet(), @CRLF, '')
Global Const $g_aArray = _StringSplitEqual($g_sText, $g_iSize)
Global $g_sResult = _ArrayToString($g_aArray, $g_sDelimiter)
$g_sResult = $g_sStart & $g_sResult & $g_sEnd
ConsoleWrite($g_sResult)
Returns:
$sText = "AutoIt v3 " _
& "is a freew" _
& "are BASIC-" _
& "like scrip" _
& "ting langu" _
& "age design" _
& "ed for aut" _
& "omating th" _
& "e Windows " _
& "GUI and ge" _
& "neral scri" _
& "pting."