VBA 字符串空间:首选方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26595384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:00:26  来源:igfitidea点击:

VBA String Spaces: Preferred Method?

stringvbaconcatenationspace

提问by RHDxSPAWNx

When concatenating strings, there are multiple valid methods of inserting spaces.

连接字符串时,有多种插入空格的有效方法。

Space Function:

空间功能:

Print "A" & Space(1) & "B"
A B

VS

VS

Quotes:

引号:

Print "A" & " " & "B"
A B

VS

VS

Chr:

铬:

Print "A" & Chr(32) & "B"
A B


All three methods yield the same result. The only place where I found Space beneficial was when using a variable for the number of spaces.

所有三种方法都会产生相同的结果。我发现 Space 有益的唯一地方是使用变量表示空格数。

Space( <expression of number - len(var)> )

Can anyone provide any insight on why one method is better than another?

谁能提供有关为什么一种方法比另一种方法更好的见解?

回答by Alex

As suggested, they all yield the same result when compiled. There might be situational benefit such as:

正如所建议的,它们在编译时都会产生相同的结果。可能有情境优势,例如:

1) Space()- Good when multiple space is needed.
Space(100)is better than chr(32) & chr(32) & chr(32) & ...

1) Space()- 当需要多个空间时很好。
Space(100)chr(32) & chr(32) & chr(32) & ...

2) " "- Visual expression that is easy to understand, esp for new vba learner
EDIT: Also faster than a method call, thanks to Blackhawk to point out

2) " "- 易于理解的视觉表达,特别是对于新的 vba 学习者
编辑:也比方法调用更快,感谢 Blackhawk 指出

3) chr()- character expression that could be used to print particular character to say a text file. E.g. printing Enter/Returnwith chr(10). I don't see any obvious benefit for print space however

3) chr()- 可用于打印特定字符以表示文本文件的字符表达式。例如用chr(10)打印Enter/Return。但是,我没有看到打印空间有任何明显的好处

回答by RubberDuck

Using a string literal should be the fastestof the bunch because the other two are functions. As you pointed out, it's easy to understand, but as @Alex pointed outSpace()is definitely better when you need more than a single space. I would always prefer the literal or space()over chr(32). chr(32)is pretty obscure for the maintainer to grok.

使用字符串文字应该是最快的,因为其他两个是函数。正如您所指出的,这很容易理解,但正如@Alex 所指出的那样,Space()当您需要多个空间时肯定会更好。我总是更喜欢文字或space()over chr(32)chr(32)维护人员很难理解。

There is another option you didn't consider though. Creating a constant.

还有一个你没有考虑过的选择。创建常量。

Const space As String = " "

This has all of the readability of space(1)with the performance of the literal " ". As an added benefit, there is only truly one instance of the constant in memory, where as the literal will show up once for each instance.

这具有space(1)文字性能的所有可读性" "。作为一个额外的好处,内存中只有一个常量实例,因为每个实例的文字都会出现一次。



If you do decide to use chr(32), it would be beneficial to use to the string version of it: chr$(32). The regular version returns a variant, whereas the string version returns a string. Variants have some extra overhead that can be avoided here.

如果您决定使用chr(32),那么使用它的字符串版本将是有益的:chr$(32)。常规版本返回一个变体,而字符串版本返回一个字符串。变体有一些额外的开销,在这里可以避免。

回答by jeffrey_t_b

Maybe there is a performance difference? You can test it yourself! See the the code at this answer related to the performance countersto get a useful VBA timer class.

也许有性能差异?你可以自己测试一下!请参阅此答案中与性能计数器相关的代码以获取有用的 VBA 计时器类。

Here's an example of this type of timing in (Excel-ish) VBA:

这是(Excel-ish)VBA中这种类型的计时示例:

Option Explicit

Sub DoTimings()
    Dim oTimer As New CTimer
    Dim fTimer(3) As Double

    Dim iiter As Long
    Const ITERATIONS As Long = 10000

    oTimer.StartCounter
    For iiter = 1 To ITERATIONS
        Debug.Print "A" & Space(1) & "B"
    Next
    fTimer(1) = oTimer.TimeElapsed

    oTimer.StartCounter
    For iiter = 1 To ITERATIONS
        Debug.Print "A" & " " & "B"
    Next
    fTimer(2) = oTimer.TimeElapsed

    oTimer.StartCounter
    For iiter = 1 To ITERATIONS
        Debug.Print "A" & Chr(32) & "B"
    Next
    fTimer(3) = oTimer.TimeElapsed

    Range("A1").Value = "Spaces: "
    Range("B1").Value = fTimer(1)
    Range("A2").Value = "Quotes: "
    Range("B2").Value = fTimer(2)
    Range("A3").Value = "Chr: "
    Range("B3").Value = fTimer(3)

End Sub

When I run that on my machine, the differences are negligible or dominated by other factors (most notably the I/O to whatever you are printing to). So it is pretty safe to say that there isn't any obvious performance difference, and you can use whatever makes the most sense from a maintainability perspective.

当我在我的机器上运行它时,差异可以忽略不计或受其他因素支配(最显着的是您打印到的任何 I/O)。因此可以肯定地说,没有任何明显的性能差异,您可以使用从可维护性角度来看最有意义的任何东西。