String concatenation VS string format

后端 未结 4 2342
無奈伤痛
無奈伤痛 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条回答
  •  无人及你
    2021-02-19 05:57

    Format works with internationalization, making it possible to localize your app. Concatenation does not. Hence, I favor format for any display which may have to be produced in a culture-dependent manner.

    Update: The reason format works for internationalization is that not all languages express everything in the same order. A contrived example would be:

    resourcestring
        sentence = ' is ';
    
    var
        subject = 'Craig';
        adjective = 'helpful';
    begin
       WriteLn(subject + sentence + adjective + '!');
    

    This works, and I can customize with a resourcestring, but in Spanish I would write, "¡Qué servicial es Craig!" The resourcestring doesn't help me. Instead I should write:

    resourcestring
        sentence = '%S is %S!'; // ES: '¡Qué %1:S es %0:S!'
    

提交回复
热议问题