tags:

views:

177

answers:

4

I know we can use the charAt() method in Java get an individual character in a string by specifying its position. Is there an equivalent method in C#?

+9  A: 
string sample = "ratty";
Console.WriteLine(sample[0]);

And

Console.WriteLine(sample.Chars(0));
Reference: http://msdn.microsoft.com/en-us/library/system.string.chars%28v=VS.71%29.aspx

The above is same as using indexers in c#.

shahkalpesh
You beat me to it -- you get the upvote.
Zach
+7  A: 

You can index into a string in C# like an array, and you get the character at that index.

Example:

In Java, you would say str.charAt(8); In C#, you would say str[8];

Zach
A: 

Console.WriteLine allows the user to specify a position in a string.

See sample:

string str = "Tigger"; Console.WriteLine( str[0] ); //returns "T"; Console.WriteLine( str[2] ); //returns "g";

There you go!

AndrewJames57
this topic, and your answer, have nothing to do with WriteLine...
Jonathan
A: 

I really don't know if it helps anything at all but I'll be damned if I don't try. I had a similar problem when I required a specific position/index, and LINQ Saved the day actually, I just wish I remembered where I put that blasted code. Sorry for the crappy answer, but it might be a good starting point if ALL ELSE fails.

Proclyon