如何将单个字符转换为字符串? [英] How do I convert a single char to a string?
本文介绍了如何将单个字符转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想枚举字符串
,而不是返回 chars
,我想使用迭代变量的类型为 string
。
I'd like to enumerate a string
and instead of it returning chars
I'd like to have the iterative variable be of type string
. This probably isn't possible to have the iterative type be a string
so what is the most efficient way to iterate through this string?
我是否需要在循环的每次迭代中创建一个新的 string
对象,或者我可以执行转换吗?
Do I need to create a new string
object with each iteration of the loop or can I perform a cast somehow?
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
}
推荐答案
使用.ToString()方法
Use the .ToString() Method
String myString = "Hello, World";
foreach (Char c in myString)
{
String cString = c.ToString();
}
这篇关于如何将单个字符转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文