拆分大字符串 [英] Split large string

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

问题描述

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

"numbercharswillbe10" &_ "othernumbersofcharwillbe10" &_ 等等.

我如何用 & 分割它_ -分隔符?

解决方案

字符串连接

根据 文档 - 语言参考 - 运算符::><块引用>

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

&= 连接赋值.

示例:

Global $g_sText = "Long " &字符串" &这里."&@CRLF$g_sText &= "更多文字."&@CRLF控制台写入($g_sText)

多行语句

根据 文档 - 语言参考 - 评论(强调添加,因为它会导致提到的未终止的字符串"错误):

<块引用>

虽然每行只允许一个语句,但如果在断"行的末尾放置一个以空格开头的下划线_",则长语句可以跨越多行.字符串定义不能分成多行,需要串联.

示例:

全局常量 $g_sText = "Long " &_字符串" &_这里."&_@CRLF &_更多文字."&_@CRLF控制台写入($g_sText)

双引号

根据文档 - 常见问题 - 双引号:<块引用>

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

或使用单引号代替...

示例来自源代码.

默认值和限制

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

<块引用>

4095 一行脚本的最大大小.

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

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

<块引用>

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

根据 文档 - 介绍 - Unicode 支持:><块引用>

AutoIt 的一些部分尚不完全支持 Unicode.它们是:

  • Send 和 ControlSend - 而是使用 ControlSetText 或剪贴板函数.
  • 控制台操作转换为 ANSI.
  • 替代方案

    硬编码的替代方案包括ClipGet()FileRead().

    剪贴板中的文本

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

    全局常量 $g_sText = ClipGet()ConsoleWrite($g_sText & @CRLF)

    来自文件的文本

    示例(先创建C:my_long_string.txt):

    #include 全局常量 $g_sFile = 'C:my_long_string.txt'全局常量 $g_sText = _TextFromFile($g_sFile)ConsoleWrite($g_sText & @CRLF)Func _TextFromFile(Const $sFile)本地 $hFile = FileOpen($sFile, $FO_READ + $FO_UTF8_NOBOM)本地常量 $sData = FileRead($hFile)文件关闭($hFile)返回 $sData结束函数

    分割字符串

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

    结构

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

    • 单个字符(在空分隔符上),
    • 单词(在空格 分隔符上)或
    • 行(在 @CRLF@LF@CR 分隔符上).

    等长

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

    #include 全局常量 $g_iSize = 10全局常量 $g_sText = ClipGet()全局常量 $g_aArray = _StringSplitEqual($g_sText, $g_iSize)_ArrayDisplay($g_aArray)Func _StringSplitEqual(Const $sText, Const $iSize = 1)本地常量 $iLength = StringLen($sText)局部常量 $iParts = Ceiling($iLength/$iSize)本地常量 $iRest = -1;$iLength - ($iSize * Floor($iLength/$iSize))本地 $iStart = 0本地 $iCount = 0本地 $aArray[$iParts]对于 $i1 = 0 到 $iParts - 1$iStart = ($i1 * $iSize) + 1$iCount = ($i1 <$iParts - 1) ?$iSize : ($iRest ? $iRest : $iSize)$aArray[$i1] = StringMid($sText, $iStart, $iCount)下一个返回 $aArray结束函数

    加入字符串

    根据文档:

    <块引用>

    _ArrayToString
    将一维或二维数组的元素放入单个字符串中,由指定的分隔符分隔

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

    #include 全局常量 $g_iSize = 10全局常量 $g_sStart = '$sText = "'全局常量 $g_sEnd = '"' & @CRLF全局常量 $g_sDelimiter = '" _' & @CRLF & ' & "'全局常量 $g_sText = StringReplace(ClipGet(), @CRLF, '')全局常量 $g_aArray = _StringSplitEqual($g_sText, $g_iSize)全局 $g_sResult = _ArrayToString($g_aArray, $g_sDelimiter)$g_sResult = $g_sStart &$g_sResult &$g_sEndConsoleWrite($g_sResult)

    返回:

    $sText = "AutoIt v3 " _&是自由人"_&是基本的-"_&像稿子"_&听语"_&年龄设计"_&ed for aut"_&omating th"_&电子视窗"_&GUI 和 ge"_&neral scri"_&叮."

    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.

    &= 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."
    

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

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