简体   繁体   English

查看字符串中的每个字符

[英]Look at each character in a string

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 ToUpper and ToLower validation and such.只是一个非常非常基本的示例,我可以添加ToUpperToLower验证等。

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 String class :下面是String class的签名:

[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

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM