String concatenation VS string format

后端 未结 4 2343
無奈伤痛
無奈伤痛 2021-02-19 05:23

What is the best approach, simple string concatenation or string.format?

For instance, what is the better to use:

 s:=v1+\' \'+v2 
         


        
4条回答
  •  梦毁少年i
    2021-02-19 06:16

    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!)

    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.

提交回复
热议问题