C#.NET和sprintf语法 [英] c#.NET and sprintf syntax
本文介绍了C#.NET和sprintf语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将这些代码转换为C#,特别是如何在C#中实现 sprintf
?
How would this code be translated into C#, specifically how would sprintf
be implemented in C#?
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
,我希望看到用户未登录"
.>
I'm expecting to see "The user isn't logged in"
if TheUser.CheckStatus()
is false
.
推荐答案
签出 string.Format ,这是使用它的代码版本:
Check out string.Format and here is a version of your code using it:
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;
或更简单地说:(使用三元表达式)
Or more simply: (using a ternary expression)
string output = "The user {0} logged in";
return TheUser.CheckStatus()
? string.Format(output, "is")
: string.Format(output, "isn't");
这篇关于C#.NET和sprintf语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文