这种字符串操作可以吗?
public string Format { get { return string.Format("{0}{1}v{1}", LastManStanding ? "FFA " : string.Empty, m_Teams.PlayersPerTeam); } }
我应该使用 StringBuilder 吗?
我不太确定这样有条件地格式化字符串有多么错误,而不是这样做
public string Format
{
get
{
StringBuilder sb = new StringBuilder();
if(LastManStanding)
sb.Append("FFA ");
sb.Append(string.Format("{0}v{0}", m_Teams.PlayersPerTeam);
return sb.ToString();
}
}
public string Format { get { return string.Format("{0}{1}v{1}", LastManStanding ? "FFA " : string.Empty, m_Teams.PlayersPerTeam); } }
should I rather use a StringBuilder
?
I'm not really sure how wrong it's to conditionally format strings like that, rather than doing
public string Format
{
get
{
StringBuilder sb = new StringBuilder();
if(LastManStanding)
sb.Append("FFA ");
sb.Append(string.Format("{0}v{0}", m_Teams.PlayersPerTeam);
return sb.ToString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
同意大家的意见——对于这么小的字符串来说是可以的。请注意,使用 StringBuilder 重写会更糟糕,因为您使用 String.Format 而不是 StringBuilder.AppendFormat。
在您的情况下,如果您决定开始用其他语言本地化您的程序,那么使用单一格式字符串创建结果字符串是有益的 - 可以从资源中提取格式化字符串,但是很难使用 StringBuilder 编写代码来尊重所有可能的情况语言。
实际上,故事的另一面可能与使用 StringBuilder 或 String.Format 无关。有些人/团队不想使用
? :
运算符。了解为什么建议您重写一段代码将有助于确定重写目标。Agree with everyone - for such small string it is ok. Note that your rewrite with StringBuilder is worse since you use String.Format instead of StringBuilder.AppendFormat.
In your case using single format string to create resulting string is benificial if you ever decide to start localizing your program in other laguages - it is possible to pull formatting tring from resources, but it is very hard to make code with StringBuilder to respect all possible languages.
Actually there could be another side of the story not realted to using StringBuilder or String.Format. Some people/teams prefer not to use
? :
operator. Knowing why you were recommended to rewrite piece of code would help to target rewrite.我会将 FFA 和普通游戏的格式化逻辑分开:
这样更容易阅读。从逻辑上讲,格式字符串也是分开的。您很可能会单独更改它们,但我很惊讶您没有这样做。 “FFA 2v2”对我来说没有多大意义。
I'd separate the formatting logic for FFA and normal games:
It's much easier to read. And logically the format strings are separate too. It's quite likely that you'll change them separately, and I'm surprised you didn't. "FFA 2v2" doesn't make much sense to me.
错了,没有。可读吗?嗯,
一点点格式化就能大有帮助
Wrong, no. Readable? Meh
A little formatting can go a long way
我认为第一个版本是首选,它清楚地表达了您的意图,并且比第二个冗长的版本更加简洁和易于阅读。我会这样格式化它:
The first version is preferred in my opinion, it clearly expresses your intention and its much more concise and easy to read then the second, lengthy one. I would format it like this though:
如果您要对长字符串进行编辑,则首选 StringBuilder,在这种情况下您不需要它。
StringBuilder is preferred if you're doing edits on long string, in this case you don't need it.
对于这么小的字符串,你所做的一切都很好。
当您处理数千个串联和/或字符串格式时,请使用 StringBuilder。
旁注:
代替:
您可以这样做:
For such small strings, what you are doing is fine.
Use
StringBuilder
when you are dealing with thousands of concatenations and/or string formatting.Side note:
Instead of:
You can do: