代码之家  ›  专栏  ›  技术社区  ›  surendra

有办法确定绳子的尺寸吗

  •  1
  • surendra  · 技术社区  · 7 年前

    有没有方法可以确定字符串的字符数。

    Sub check ()
        logfilepath = "D:\surendra\VBScript\File_Compare\log.txt"
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        set logFile = objFSO.CreateTextFile(logfilepath, true)
        myarray = Array(1,2.0,5.83,100)
        For Each element In myarray
        logFile.WriteLine "       " & element & "       " & 0 
        Next
       logFile.Close
    End Sub
    

    上面的代码是

       1       0
       2       0
       5.83       0
       100       0
    

    但我想以

       1       0
       2       0
    5.83       0
     100       0
    

    应根据字符数调整空格,以得到总共8个字符的长度。

    在tcl中修复字符串格式命令的字符长度的任何命令。

    3 回复  |  直到 7 年前
        1
  •  0
  •   Pankaj Jaju    7 年前

    更改此行--> logFile.WriteLine " " & element & " " & 0

    对…… logFile.WriteLine element & String(8-Len(element)," ") & 0

    我已经用过 String 函数根据每个元素的长度添加空格。

        2
  •  0
  •   Matthew Martin    7 年前

    在您的writeline上,使用format@x8右对齐&左到第8个字符来剪裁字符串,即。

    logFile.WriteLine "      |" & Format(Left(element,8), "@@@@@@@@") & "|      " & 0
    

    参考文献:

    String Manipulation

    Format

        3
  •  0
  •   JosefZ    7 年前
    logFile.WriteLine "       " & element & "       " & 0
    

    logFile.WriteLine Right( Space(8) & CStr( element),8) & "       " & 0
    

    检查 Functions (VBScript) reference 对于 Right , Space CStr 功能详细说明。你可能对 FormatNumber Function 也。

    但是,取而代之的是vbscript, you should begin learning Windows PowerShell 因为Windows Powershell是管理脚本的未来(至少对于Microsoft Windows操作系统)。