分割大字串 [英] Split large string

查看:119
本文介绍了分割大字串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长文本,需要将其转换为小字符串,以便可以将其包含在AutoIt脚本中.如果我包含多行文本,它将显示error unterminated string.所以我应该有:

I have a long text which needs to be converted to small strings so I can include it to an AutoIt script. If I include multi-line text it shows error unterminated string. So I should have:

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

如何使用& _-定界符分割它?

How can I split it with & _ -delimiters?

推荐答案

字符串串联

根据文档-语言参考-运算符:

String concatenation

As per Documentation - Language Reference - Operators:

&串联/连接两个字符串.

& Concatenates/joins two strings.

&=串联分配.

示例:

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.

示例:

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

ConsoleWrite($g_sText)

双引号

根据文档-常见问题解答-双引号:

如果要在字符串中使用双引号,则必须将它们加倍".因此,对于每个报价,您都应该使用两个. ...

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

示例可从源头获得.

根据文档-附录-限制/默认值:

4095一行脚本的最大大小.

4095 Maximum size for a line of script.

2,147,483,647最大字符串长度.

根据文档-语言参考-数据类型-字符串:

所有AutoIt字符串都使用UTF-16(实际上更确切地说是UCS-2)编码.

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

根据文档-简介-Unicode支持:

AutoIt的某些部分尚未完全支持Unicode.这些是:

  • 发送和ControlSend-而是使用ControlSetText或剪贴板函数.
  • 控制台操作将转换为ANSI.
  • 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().

    示例(首先选择并复制文本 CTRL + C ):

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

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

    文件中的文本

    示例(首先创建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
    

    分割字符串

    硬编码手动字符串拆分的替代方法包括 StringSplit() _StringExplode() ( StringMid() .

    Split string

    Alternatives to hard coded manual string splitting include StringSplit(), _StringExplode() (related) and StringMid().

    StringSplit()将字符串拆分为以下数组:

    StringSplit() splits a string into array of:

    • 单个字符(在空定界符上),
    • 单词(在空格分隔符上)或
    • 行(在@CRLF@LF@CR分隔符上).
    • individual characters (on empty delimiter),
    • words (on space delimiter) or
    • lines (on @CRLF, @LF or @CR delimiter).

    StringMid()返回字符串的一部分.可以用来分割成相等长度的部分.示例(无错误检查,首先选择并复制文本 CTRL + C ):

    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
    

    加入字符串

    根据文档:

    _ArrayToString
    将1D或2D数组的元素放入单个字符串中,并由指定的定界符分隔

    _ArrayToString
    Places the elements of a 1D or 2D array into a single string, separated by the specified delimiters

    示例(添加_StringSplitEqual()并首先选择并复制文本 CTRL + C ):

    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)
    

    返回:

    $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."
    

    这篇关于分割大字串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆