我想列舉一個string
而不是它返回chars
我想重複變量是string
類型。這可能是不可能的迭代類型是string
所以迭代通過這個字符串的最有效的方法是什麼?如何將單個字符轉換爲字符串?
我是否需要在循環的每次迭代中創建一個新的string
對象,或者我能否以某種方式執行演員?
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
}
考慮使用C#類型'string'和'char'來代替CLR類型。最後相同,但可以說在C#中更正確。 – andleer
@andleer你是對的,我剛剛修改了OP的示例代碼來顯示ToString方法。 –
評論是針對他,而不是你。別擔心。 – andleer