[英]C# equivalent to Java's charAt()?
I know we can use the charAt()
method in Java get an individual character in a string by specifying its position. 我知道我们可以在Java中使用
charAt()
方法通过指定其位置来获取字符串中的单个字符。 Is there an equivalent method in C#? C#中是否有等效的方法?
You can index into a string in C# like an array, and you get the character at that index. 您可以像在数组中一样在C#中索引字符串,并获得该索引处的字符。
Example: 例:
In Java, you would say 在Java中,你会说
str.charAt(8);
In C#, you would say 在C#中,你会说
str[8];
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
参考:
http :
//msdn.microsoft.com/en-us/library/system.string.chars%28v=VS.71%29.aspx
The above is same as using indexers in c#. 以上与在c#中使用索引器相同。
you can use LINQ 你可以使用LINQ
string abc = "abc";
char getresult = abc.Where((item, index) => index == 2).Single();
please try to make it as a character 请尝试将其作为角色
string str = "Tigger";
//then str[0] will return 'T' not "T"
Console.WriteLine allows the user to specify a position in a string. Console.WriteLine允许用户指定字符串中的位置。
See sample: 见样本:
string str = "Tigger"; string str =“Tigger”; Console.WriteLine( str[0] );
Console.WriteLine(str [0]); //returns "T";
//返回“T”; Console.WriteLine( str[2] );
Console.WriteLine(str [2]); //returns "g";
//返回“g”;
There you go! 你去!
Simply use String.ElementAt()
. 只需使用
String.ElementAt()
。 It's quite similar to java's String.charAt()
. 它与java的
String.charAt()
非常相似。 Have fun coding! 玩得开心!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.