2013-01-23 9 views
10

Come si tradurrebbe questo codice in C#, in particolare come sarebbe implementato sprintf in C#?sintassi C# .NET e 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; 

mi aspetto di vedere se "The user isn't logged in"TheUser.CheckStatus() è false.

risposta

15

Partenza string.Format e qui è una versione del codice di usarlo:

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; 

O più semplicemente: (utilizzando un'espressione ternaria)

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

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

Grazie per il ref! – Jimmyt1988

+2

Oppure come un singolo liner: 'return string.Format (" L'utente {0} ha effettuato l'accesso ", TheUser.CheckStatus()?" È ":" non è ");' – Stormenet

6

L'intera famiglia di funzioni printf in C è sostituita da String.Format. La stessa interfaccia è anche esposta ad esempio Console.WriteLine().

string output = "The user {0} logged in"; 
string loggedIn = "is"; 
string loggedOut = "isn't"; 


output = string.Format(output, loggedIn); 
3

string.Format in soccorso

string output = "The user {0} logged in"; 
string loggedIn = "is"; 
string loggedOut = "isn't"; 

output = (TheUser.CheckStatus() ? string.Format(output, loggedIn) : 
            string.Format(output, loggedOut)); 
return output; 

Vedi anche questo articolo molto fondamentale composite formatting

EDIT: più breve

return string.Format(output, (TheUser.CheckStatus() ? loggedIn : loggedOut)); 
+0

sprintf ... obbediscimi o ti metterò sul pannolino !! – Jimmyt1988

+0

I tuoi '()' sono sbilanciati. –

+0

@HenkHolterman ringrazia – Steve

3

Se si vuole attaccare con% s,% d ....

string sprintf(string input,params object[] inpVars) 
{ 
    int i=0; 
    input=Regex.Replace(input,"%.",m=>("{"+ ++i +"}")); 
    return string.Format(input,inpVars); 
} 

È ora possibile fare

sprintf("hello %s..Hi %d","foofoo",455); 
+0

Grazie per l'alternativa. Buono a sapersi! – Jimmyt1988

+1

Ne avevo bisogno :), grazie .. Inoltre, non dimenticare di includere 'using System.Text.RegularExpressions;' – Sourav

4

Con C# 6 è possibile utilizzare la stringa componibile:

if (TheUser.CheckStatus()) 
{ 
    output = $"The user {loggedIn} logged in" 
} 

Lo {loggedIn} all'interno della stringa è il nome della variabile che è stato definito.

Inoltre, si dispone di intelligenza all'interno delle parentesi graffe per selezionare il nome della variabile.

+0

È possibile "caricare" una stringa di questo tipo? Ad esempio, ottenere la stringa da qualche altra parte, e quindi in qualche modo usarla nel codice? – htmlcoderexe

0

Anirudha ha già risolto la soluzione ma non può aggiungere commenti in modo da postare la risposta. Deve essere int i=-1; o farà eccezione.

string sprintf(string input,params object[] inpVars) 
{ 
    int i=-1; 
    input=Regex.Replace(input,"%.",m=>("{"+ ++i +"}")); 
    return string.Format(input,inpVars); 
}