String error

Carlos's Avatar, Join Date: Dec 2008
Contributor
Hello everyone!

Please! check this code.

string word="It is a string";
while (word[i] != '\0')
{
Console.Write(word[i]+"\n");
i++;
}

It give an unexpected error, after printing all the letters.
Can anyone tell me how to remove this error?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
In C# string is not character array which you can and should loop through
0
Carlos's Avatar, Join Date: Dec 2008
Contributor
So, how can i break this string into characters without error.
string word;
Console.Write(word[i]+"\n");

Please! Write some code for it.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Could this be what you're looking for:
http://msdn.microsoft.com/en-us/libr...substring.aspx
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Do you want to break it up into characters or print the entire string?
0
Carlos's Avatar, Join Date: Dec 2008
Contributor
I want to break the string into characters, by using while or For loop, and the loop should stop as the string ends.
My Code is breaking the string but it give error in the last.

I can also use the condition word.length() but i want to do it as it is done in C++.
while (word[i] != '\0')
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Try the Substring function o the [] operator but do not loop it like in C.

Loop it using the length variable