c#读取指定字符后的字符
As we know that, Console.ReadLine() is used for input in C#, it actually reads a string and then we convert or parse it to targeted type.
众所周知, Console.ReadLine()用于C#中的输入,它实际上是读取一个字符串,然后我们将其转换或解析为目标类型。
In this tutorial, we will learn how to read a character in C#?
在本教程中,我们将学习如何在C#中读取字符?
在C#中读取/输入单个字符的方法 (Methods to read/input single character in C#)
Following methods can be used to read a character:
可以使用以下方法来读取字符 :
Using Console.ReadLine()[0]
使用Console.ReadLine()[0]
Using Console.ReadKey().KeyChar
使用Console.ReadKey()。KeyChar
Using Char.TryParse()
使用Char.TryParse()
Using Convert.ToChar()
使用Convert.ToChar()
1)使用Console.ReadLine()[0]输入字符 (1) Character input using Console.ReadLine()[0])
It's very simple, as we know that Console.ReadLine() reads a string and string is the set of characters. So we can use this method and extract its first character using 0th Index ([0]). In this case, we can input a single character and string also – it will return only first character.
很简单,因为我们知道Console.ReadLine()读取一个字符串,而string是字符集。 因此,我们可以使用此方法并使用第 0 个索引( [0] )提取其第一个字符。 在这种情况下,我们也可以输入单个字符和字符串-它只会返回第一个字符。
Syntax:
句法:
char_variable = Console.ReadLine()[0];