Split a large string

I have a long text that needs to be converted into small lines, so I can include it in an AutoIt script. If I include multiline text, it shows error unterminated string . Therefore, I had to:

 "numbercharswillbe10" &_ "othernumbersofcharwillbe10" &_ etc.. 

How can I split it into & _ -delimiters?

0
source share
1 answer

String concatenation

By Documentation - Language Reference - Operators :

& Joins / joins two lines.

&= Concatenation purpose.

Example:

 Global $g_sText = "Long " & "string " & "here." & @CRLF $g_sText &= "More text." & @CRLF ConsoleWrite($g_sText) 

Multiple Line Operators

According to the Documentation - Language Reference - Comments (highlighted because it causes the mentioned "unterminated string" error):

Although only one statement per line is allowed, a long statement can span multiple lines if the underscore " _ ", which is preceded by a space, is placed at the end of the "broken" line. The definition of a string cannot be divided into several lines; you must use concatenation.

Example:

 Global Const $g_sText = "Long " & _ "string " & _ "here." & _ @CRLF & _ "More text." & _ @CRLF ConsoleWrite($g_sText) 

Double quotes

By Documentation - Frequently Asked Questions - Double quotes :

If you want to use double quotes inside the string, you must double them. Therefore, for each quote you want to use two ....

or use single quotes instead ...

Examples are available from the source.

Default Values ​​and Limits

By Documentation - Appendix - Limits / Defaults :

4095 maximum size for a script string.

2,147,483,647 Maximum line length.

By Documentation - Language Reference - Data Types - Strings :

All AutoIt strings use UTF-16 encoding (in fact, and more precisely, UCS-2).

By Documentation - Introduction - Unicode Support :

There are several AutoIt parts that do not yet have full Unicode support. It:

Send and ControlSend - Use the ControlSetText or Clipboard functions instead. Console operations are converted to ANSI.

Alternatives

Alternatives to ClipGet() include ClipGet() and FileRead() .

Clipboard text

Example (first select and copy the text CTRL + C ):

 Global Const $g_sText = ClipGet() ConsoleWrite($g_sText & @CRLF) 

Text from file

Example (first create C:\my_long_string.txt ):

 #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 line

Alternatives to hard-coding manual string splitting include StringSplit() , _StringExplode() (and StringMid() .

Structural

StringSplit() splits a string into an array:

  • individual characters (on an empty delimiter),
  • words (space ) or
  • (in @CRLF , @LF or @CR ).

Equal length

StringMid() returns the portion of the string. It can be used to divide into pieces of equal length. Example (no error checking, first select and copy the text CTRL + C ):

 #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 

Append string

According to the documentation :

_ArrayToString
Places 1D or 2D array elements on a single line, separated by specified delimiters

Example (add _StringSplitEqual() and first select and copy the text CTRL + C ):

 #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) 

Return:

 $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." 
+3
source

Source: https://habr.com/ru/post/907617/


All Articles