簡體   English   中英

查看字符串中的每個字符

[英]Look at each character in a string

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

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

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

使用StringBuilder

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

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

下面是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

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

……那是你需要的嗎?
字符串是IEnumerable<char>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM