我想列举一个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