String.Format或不是? [英] String.Format or Not?

查看:167
本文介绍了String.Format或不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重复自:字符串输出:格式或concat在C#?



特别是在C#世界中使用String.Format的一切都很常见,通常作为VB.NET开发人员,除非我有*我不String.Format,



我喜欢正常的字符串连接,例如:

  V1 = V2& test-x& V3&  - ; 

对我来说比这更好:

  V1 = String.Format({0} test-x {1}  - ;,V2,V3)

我错过了什么?

使用String.Format的原因(从答案)我会尝试




  • 如果您使用字符串格式,本地化会容易得多。

  • 更好的效果


**有时我需要更改样式或动态替换,然后使用String.Format *

解决方案

如果你打算本地化你的应用程序(通常很难在开始时做出裁决),那么String.Format就是因为两个原因:


  1. 您只有一个字符串文字可以翻译

  2. 可以更改值的顺序,这在其他语言中可能更有意义。


Duplicate from : String output: format or concat in C#?

Especially in C# world using String.Format for everything is really common, normally as VB.NET developer unless I have to* I don't String.Format,

I prefer normal string concatenation, such as:

V1 = V2 & "test-x" & V3 & "-;"

to me it's better than this:

V1 = String.Format("{0} test-x {1} -;", V2, V3)

Am I missing something? Or is this just a personal preference?

Reasons to Use String.Format (From The Answers) (I'll try to keep this up to date)

  • Localization is so much easier if you use String Format
  • Obviously it's easier to change the format of input
  • It's more readable (however this is personal)
  • Better Performance

**Sometimes I need to change the style or replacing stuff dynamically then I use String.Format*

解决方案

If you're ever going to localize your application (and it's often hard to rule that out at the start), then String.Format is to be much preferred, for two reasons:

  1. You have only one string literal to translate
  2. You can change the order of the values, which may make more sense in another language.

这篇关于String.Format或不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆