Vorrei elencare un string
e invece di restituirlo chars
Mi piacerebbe avere la variabile iterativa di tipo string
. Questo probabilmente non è possibile che il tipo iterativo sia un string
, quindi qual è il modo più efficace per scorrere questa stringa?Come posso convertire un singolo carattere in una stringa?
Devo creare un nuovo oggetto string
con ogni iterazione del ciclo o posso eseguire un cast in qualche modo?
String myString = "Hello, World";
foreach (Char c in myString)
{
// what I want to do in here is get a string representation of c
// but I can't cast expression of type 'char' to type 'string'
String cString = (String)c; // this will not compile
}
Considerare l'uso dei tipi C# 'stringa' e' carattere' invece dei tipi CLR. Lo stesso alla fine, ma probabilmente più corretto in C#. – andleer
@andleer Hai ragione, ho appena modificato il codice di esempio di OP per mostrare il metodo ToString. –
Il commento era diretto a lui, non tu. Nessun problema. – andleer