【问题标题】:Why does my script return 0?为什么我的脚本返回 0?
【发布时间】:2018-07-06 07:35:04
【问题描述】:

这个 AutoIt 脚本计算字数;字符(带和不带空格);线条;以及用户选择的文本的预计说话时间(假设每秒两个单词)。

但是,如果输入字符串的起始位置是0(在文件顶部的左上角),则该方法对所有内容都返回0,即使主函数运行良好。

我不知道为什么。

$input_str = GUICtrlRead($Textbox)
$selpos = _GUICtrlEdit_GetSel($Textbox)
MsgBox($MB_OK, $selpos[0], $selpos[1])
$selstring = StringMid($input_str, $selpos[0], ($selpos[1] - $selpos[0]))
$WordArray = StringRegExp($selstring, "[\s\.:;,]*([a-zA-Z0-9-_]+)[\s\.:;,]*", 3)
$SingleQuotes = StringRegExp($selstring, "'", 3)
$Result = ""
$Seconds = (UBound($WordArray) - UBound($SingleQuotes)) / 2

If $Seconds >= 3600 Then
    If $Seconds / 3600 >= 2 Then
        $Result = $Result & Int($Seconds / 3600) & " hours "
    Else
        $Result = $Result & Int($Seconds / 3600) & " hour "
    EndIf
EndIf

If Mod($Seconds, 3600) >= 60 Then
    If $Seconds / 60 >= 2 Then
        $Result = $Result & Int(Mod($Seconds, 3600) / 60) & " minutes "
    Else
        $Result = $Result & Int(Mod($Seconds, 3600) / 60) & " minute "
    EndIf
EndIf

If Mod($Seconds, 60) > 0 Then
    If Mod($Seconds, 60) >= 2 Then
        $Result = $Result & Int(Mod($Seconds, 60)) & " seconds "
    Else
        $Result = $Result & Int(Mod($Seconds, 60)) & " second "
    EndIf
EndIf
MsgBox($MB_OK, "Selection Properties", _
    "Number of characters (with spaces): " & StringLen($selstring) & @CRLF & _
    "Number of Characters (without spaces): " & StringLen(StringStripWS($selstring, 8)) & @CRLF & _
    "Number of words: " & (UBound($WordArray) - UBound($SingleQuotes)) & @CRLF & _
    "Number of lines: " & _GUICtrlEdit_GetLineCount($selstring) & @CRLF & _
    "Estimated speaking time: " & $Result _
)

【问题讨论】:

    标签: autoit word-count textselection


    【解决方案1】:

    如果$selpos[0] = 0StringMid() 返回一个空字符串,因为您的开始超出范围(StringMid() 的第一个位置是1)。

    $sTest = "A sample test string"
    
    MsgBox(0, '' , StringMid($sTest , 0 , 8))
    MsgBox(0, '' , StringMid($sTest , 1 , 8))
    

    【讨论】:

      猜你喜欢
      • 2012-11-12
      • 2013-01-26
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2022-01-23
      相关资源
      最近更新 更多