C# 查看字符串中的每个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10058854/
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-09 12:17:32  来源:igfitidea点击:

Look at each character in a string

c#stringsearchcharacter

提问by user1290653

I was wondering if anyone knew how to look through a string at each character and then add each character to a new string? Just a really really basic example, I can add the ToUpperand ToLowervalidation and such.

我想知道是否有人知道如何查看每个字符的字符串,然后将每个字符添加到新字符串中?只是一个非常基本的示例,我可以添加ToUpperToLower验证等。

采纳答案by xandercoded

string foo = "hello world", bar = string.Empty;

foreach(char c in foo){
    bar += c;
}


Using StringBuilder:

使用StringBuilder

string foo = "hello world";
StringBuilder bar = new StringBuilder();

foreach (char c in foo)
{
    bar.Append(c);
}


Below is the signature of the Stringclass:

以下是 的签名Stringclass

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class String : IComparable, 
    ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, 
    IEnumerable, IEquatable<string>

http://msdn.microsoft.com/en-us/library/system.string(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/system.string(v=vs.100).aspx

回答by NSGaga-mostly-inactive

var output = ""; // or use StringBuilder
foreach(char c in text)
{
     output += c; // if or do with c what you need
}

...is that what you needed?
string is IEnumerable<char>

……这是你需要的吗?
字符串是IEnumerable<char>