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#?
views:
177answers:
4string 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#.
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];
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!
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.