我想枚举一个字符串,而不是返回字符,我希望迭代变量的类型为字符串。可能无法将迭代类型设置为字符串,那么最有效的方法是什么来遍历这个字符串呢?
在循环的每次迭代中,我需要创建一个新的字符串对象吗,还是可以进行某种类型转换?
在循环的每次迭代中,我需要创建一个新的字符串对象吗,还是可以进行某种类型转换?
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
}
string
和char
,而不是CLR类型。最终结果相同,但在C#中更正确。 - andleerstring s = (string) c
。 - TheInitializer