C# 如何将单个字符转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13736480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:38:05  来源:igfitidea点击:

How do I convert a single char to a string?

c#stringcastingchar

提问by Ian R. O'Brien

I'd like to enumerate a stringand instead of it returning charsI'd like to have the iterative variable be of type string. This probably isn't possible to have the iterative type be a stringso what is the most efficient way to iterate through this string?

我想枚举 astring而不是它返回chars我想让迭代变量的类型为string。这可能不可能让迭代类型成为 astring那么迭代这个字符串的最有效方法是什么?

Do I need to create a new stringobject with each iteration of the loop or can I perform a cast somehow?

我是否需要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
}

采纳答案by Mark Hall

Use the .ToString() Method

使用 .ToString() 方法

String myString = "Hello, World";
foreach (Char c in myString)
{
    String cString = c.ToString(); 
}

回答by Ian R. O'Brien

It seems that the obvious thing to do is this:

似乎显而易见的事情是这样的:

String cString = c.ToString()

回答by Michael G

Create a new string from the char.

从字符创建一个新字符串。

 String cString = new String(new char[] { c });

or

或者

 String cString = c.ToString();

回答by trebuchet

Did you try:

你试过了吗:

String s = new String(new char[] { 'c' });

String s = new String(new char[] { 'c' });

回答by Jeroen

String cString = c.ToString();

回答by Lukasz Madon

You have two options. Create a stringobject or call ToStringmethod.

你有两个选择。创建string对象或调用ToString方法。

String cString = c.ToString();
String cString2 = new String(c, 1); // second parameter indicates
                                    // how many times it should be repeated

回答by horgh

Create an extension method:

创建扩展方法:

public static IEnumerable<string> GetCharsAsStrings(this string value)
{
    return value.Select(c =>
           {
                //not good at all, but also a working variant
                //return string.Concat(c);

                return c.ToString();
           });
}

and loop through strings:

并循环遍历字符串:

string s = "123456";
foreach (string c in s.GetCharsAsStrings())
{
    //...
}

回答by user1489240

Why not this code? Won't it be faster?

为什么不是这个代码?不会更快吗?

string myString = "Hello, World";
foreach( char c in myString )
{
    string cString = new string( c, 1 );
}

回答by sean

With C# 6 interpolation:

使用 C# 6 插值:

char ch = 'A';
string s = $"{ch}";

This shaves a few bytes. :)

这会减少几个字节。:)

回答by Jim Balter

probably isn't possible to have the iterative type be a string

可能不可能将迭代类型设为字符串

Sure it is:

就是这样:

foreach (string str in myString.Select(c => c.ToString())
{
...
}

Any of the suggestions in the other answers can be substituted for c.ToString(). Probably the most efficient by a small hair is c => new string(c, 1), which is what char.ToString()probably does under the hood.

其他答案中的任何建议都可以替换为c.ToString()。可能最有效的小毛是c => new string(c, 1),这char.ToString()可能是引擎盖下所做的。