Delphi相当于VB"Space"

t j*_*t j 3 delphi string

Visual Basic"Space"函数返回由指定数量的空格组成的字符串.

Visual Basic示例:

Property Get Sections() As String
Dim sBuf As String
Dim iSize As String
Dim iRetCode As Integer
    sBuf = Space$(8192)
    iSize = Len(sBuf)
    iRetCode = GetPrivateProfileString(0&, 0&, m_sDefault, sBuf, iSize, m_sPath)
    If (iSize > 0) Then
        Sections = Left$(sBuf, iRetCode)
    Else
        Sections = ""
    End If
End Property
Run Code Online (Sandbox Code Playgroud)

Delphi相当于"Space"的等价物是什么?谢谢.

Dav*_*nan 6

要回答直接问题,最接近的等价物是StringOfChar:

sBuf := StringOfChar(' ', 8192);
Run Code Online (Sandbox Code Playgroud)

虽然如果你将问题中的代码翻译成Delphi,你就不会用空格填充字符串.VB代码实际上并不对字符串在分配时包含的内容感兴趣,它只是用Space$一种简单的方法来分配一定长度的字符串.这些空格将被API调用返回的字符串覆盖.

在Delphi中,您将通过编写以下内容来分配长度为8192的字符串:

SetLength(sBuf, 8192);
Run Code Online (Sandbox Code Playgroud)

事实上,在Delphi中,您将使用TIniFile从INI文件中读取.因此,如果您实际上是在尝试翻译问题中的代码,我建议您不要这样做,而是使用Delphi的内置库.