字符串连接VS字符串格式 [英] String concatenation VS string format
问题描述
什么是最好的方法,简单的字符串连接或 string.format
?
What is the best approach, simple string concatenation or string.format
?
例如,最好使用什么:
s:=v1+' '+v2
或
s:=format('%S %S',[v1,v2])
推荐答案
取决于您的最佳标准。如果你正在做的是连接两个字符串,我会用+运算符。很明显你想要做什么和易于阅读,而且它有点快一点,因为它不必使用变体。 (你看过什么格式
实际上是在引擎盖子下吗?这有点吓人!)
Depends on your criteria for "best". If all you're doing is concatenating two strings, I'd go with the + operator. It's obvious what you're trying to do and easy to read, and it's a little bit faster because it doesn't have to use variants. (Have you looked at what format
actually does under the hood? it's kinda scary!)
主要优点格式
可以让您制作一个字符串并将其存储在某个地方,例如在文本文件或 resourcestring 中,然后再收集其他参数。这使得它对于更复杂的任务很有用。但是,如果你需要做的只是把两个字符串放在一起,那么这个有点太过分了。
The major advantage of format
is that it lets you make a single string and store it somewhere, such as in a text file or a resourcestring, and gather other parameters later. This makes it useful for more complex tasks. But if all you need to do is stick two strings together, it's kinda overkill IMO.
这篇关于字符串连接VS字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!