这个代码如何转换成C#,具体如何在C#中实现sprintf?
@H_301_7@解决方法
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;
查看
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");