Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
199 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use the .ToString() Method

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...