这种字符串操作可以吗?

发布于 2024-11-04 10:08:53 字数 506 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

那请放手 2024-11-11 10:09:07

同意大家的意见——对于这么小的字符串来说是可以的。请注意,使用 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.

无声无音无过去 2024-11-11 10:09:07

我会将 FFA 和普通游戏的格式化逻辑分开:

public string Format 
{
    get
    {
        if(LastManStanding)
            return string.Format("FFA {0}v{0}", m_Teams.PlayersPerTeam);
        else
            return string.Format("{0}v{0}", m_Teams.PlayersPerTeam);
    }
}

这样更容易阅读。从逻辑上讲,格式字符串也是分开的。您很可能会单独更改它们,但我很惊讶您没有这样做。 “FFA 2v2”对我来说没有多大意义。

I'd separate the formatting logic for FFA and normal games:

public string Format 
{
    get
    {
        if(LastManStanding)
            return string.Format("FFA {0}v{0}", m_Teams.PlayersPerTeam);
        else
            return string.Format("{0}v{0}", m_Teams.PlayersPerTeam);
    }
}

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.

鲸落 2024-11-11 10:09:06

错了,没有。可读吗?嗯,

一点点格式化就能大有帮助

public string Format 
{ 
  get 
  { 
    return string.Format("{0}{1}v{1}", 
      LastManStanding ? "FFA " : string.Empty,
      m_Teams.PlayersPerTeam); 
  } 
}

Wrong, no. Readable? Meh

A little formatting can go a long way

public string Format 
{ 
  get 
  { 
    return string.Format("{0}{1}v{1}", 
      LastManStanding ? "FFA " : string.Empty,
      m_Teams.PlayersPerTeam); 
  } 
}
虚拟世界 2024-11-11 10:09:06

我认为第一个版本是首选,它清楚地表达了您的意图,并且比第二个冗长的版本更加简洁和易于阅读。我会这样格式化它:

public string Format 
{ 
    get 
    { 
        return string.Format("{0}{1}v{1}", LastManStanding ? "FFA " : string.Empty,
                                           m_Teams.PlayersPerTeam); 
    } 
}

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:

public string Format 
{ 
    get 
    { 
        return string.Format("{0}{1}v{1}", LastManStanding ? "FFA " : string.Empty,
                                           m_Teams.PlayersPerTeam); 
    } 
}
无力看清 2024-11-11 10:09:06

如果您要对长字符串进行编辑,则首选 StringBuilder,在这种情况下您不需要它。

StringBuilder is preferred if you're doing edits on long string, in this case you don't need it.

东京女 2024-11-11 10:09:05

对于这么小的字符串,你所做的一切都很好。

当您处理数千个串联和/或字符串格式时,请使用 StringBuilder。


旁注:

代替:

sb.Append(string.Format("{0}v{0}", m_Teams.PlayersPerTeam));

您可以这样做:

sb.AppendFormat("{0}v{0}", m_Teams.PlayersPerTeam);

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:

sb.Append(string.Format("{0}v{0}", m_Teams.PlayersPerTeam));

You can do:

sb.AppendFormat("{0}v{0}", m_Teams.PlayersPerTeam);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文