加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#.NET和sprintf语法

发布时间:2020-12-15 06:53:25 所属栏目:百科 来源:网络整理
导读:这个代码如何转换成C#,具体如何在C#中实现sprintf? string output = "The user %s logged in";string loggedIn = "is";string loggedOut = "isn't";if (TheUser.CheckStatus()){ output = sprintf(output,loggedIn);}else{ output = sprintf(output,loggedO
这个代码如何转换成C#,具体如何在C#中实现sprintf?
string output = "The user %s logged in";
string loggedIn = "is";
string loggedOut = "isn't";

if (TheUser.CheckStatus())
{
    output = sprintf(output,loggedIn);
}
else
{
    output = sprintf(output,loggedOut);
}

return output;

如果TheUser.CheckStatus()为false,我希望看到“用户没有登录”.

解决方法

查看 string.Format,这里是一个使用它的代码的版本:
string output = "The user {0} logged in";
string loggedIn = "is";
string loggedOut = "isn't";

if (TheUser.CheckStatus())
{
    output = string.Format(output,loggedIn);
}
else
{
    output = string.Format(output,loggedOut);
}

return output;

或者更简单:(使用三元表达式)

string output = "The user {0} logged in";

return TheUser.CheckStatus() 
    ? string.Format(output,"is")
    : string.Format(output,"isn't");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读