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];
示例:使用Console.ReadLine()[0]读取字符的C#代码 (Example: C# code to Read a character using Console.ReadLine()[0])
// C# program to input character
// using Console.ReadLine()[0]
using System;
using System.IO;
using System.Text;
namespace IncludeHelp
{
class Test
{
// Main Method
static void Main(string[] args)
{
char ch;
//input character
Console.Write("Enter a character: ");
ch = Console.ReadLine()[0];
//printing the input character
Console.WriteLine("Input character is {0}", ch);
//hit ENTER to exit the program
Console.ReadLine();
}
}
}
Output
输出量
First run:
Enter a character: H
Input character is H
Second run:
Enter a character: Hello world
Input character is H
2)使用Console.ReadKey()。KeyChar输入字符 (2) Character input using Console.ReadKey().KeyChar)
We can also use Console.ReadKey() method to read a key and then to get the character, use KeyChar.
我们还可以使用Console.ReadKey()方法读取一个键,然后使用KeyChar来获取字符。
Console.ReadKey() – is used to obtain the next character or function key pressed by the user. The pressed key will display on the console.
Console.ReadKey() –用于获取用户按下的下一个字符或功能键。 按下的键将显示在控制台上。
KeyChar returns the Unicode character represented by the current System.ConsoleKeyInfo object.
KeyChar返回由当前System.ConsoleKeyInfo对象表示的Unicode字符。
Note: In other words, please understand – it reads a function key (a character also), displays on the console, but don't wait to press return key (i.e. ENTER).
注意:换句话说,请理解–它读取功能键(也包括一个字符),在控制台上显示,但不要等待按回车键(即ENTER)。
Syntax:
句法:
char_variable = Console.ReadKey().KeyChar;
示例:使用Console.ReadKey()。KeyChar读取字符的C#代码 (Example: C# code to Read a character using Console.ReadKey().KeyChar)
// C# program to input a character
// using Console.ReadKey().KeyChar
using System;
using System.IO;
using System.Text;
namespace IncludeHelp
{
class Test
{
// Main Method
static void Main(string[] args)
{
char ch;
//input character
Console.Write("Enter a character: ");
ch = Console.ReadKey().KeyChar;
//printing the input character
Console.WriteLine("Input character is {0}", ch);
//hit ENTER to exit the program
Console.ReadLine();
}
}
}
Output
输出量
Enter a character: HInput character is H
3)使用Char.TryParse(string,out)输入字符 (3) Character input using Char.TryParse(string, out))
Char.TryParse() method is the perfect method to read a character as it also handles the exception i.e. if an input value is not a character it will not throw any error. It returns an input status also if character input is valid – it returns true, else it returns false.
Char.TryParse()方法是读取字符的理想方法,因为它还处理异常,即,如果输入值不是字符,则不会引发任何错误。 如果字符输入有效,它也会返回输入状态–返回true ,否则返回false 。
Syntax:
句法:
bool result = Char.TryParse(String s, out char char_variable);
It stores the result in char_variable and returns a Boolean value.
它将结果存储在char_variable中,并返回一个布尔值。
示例:使用Char.TryParse()读取字符的C#代码 (Example: C# code to Read a character using Char.TryParse())
// C# program to input a character
// using Char.TryParse()
using System;
using System.IO;
using System.Text;
namespace IncludeHelp
{
class Test
{
// Main Method
static void Main(string[] args)
{
char ch;
bool result;
//input character
Console.Write("Enter a character: ");
result = Char.TryParse(Console.ReadLine(), out ch);
//printing the input character
Console.WriteLine("result is: {0}", result);
Console.WriteLine("Input character is {0}", ch);
//hit ENTER to exit the program
Console.ReadLine();
}
}
}
Output
输出量
First run:
Enter a character: A
result is: True
Input character is A
Second run:
Enter a character: Hello world
result is: False
Input character is
4)使用Convert.ToChar()输入字符 (4) Character input using Convert.ToChar())
Convert.ToChar() method converts the specified string's value to the character.
Convert.ToChar()方法将指定字符串的值转换为字符。
Note: Input value must be a single character if you input a string – it will throw an exception "String must be exactly one character long".
注意:如果输入字符串,则输入值必须是单个字符–它将引发异常“字符串必须正好一个字符长” 。
Syntax:
句法:
char_variable = Convert.ToChar(string s);
示例:使用Convert.ToChar()读取字符的C#代码 (Example: C# code to Read a character using Convert.ToChar())
// C# program to input a character
// using Convert.ToChar()
using System;
using System.IO;
using System.Text;
namespace IncludeHelp
{
class Test
{
// Main Method
static void Main(string[] args)
{
char ch;
//input character
Console.Write("Enter a character: ");
ch = Convert.ToChar(Console.ReadLine());
//printing the input character
Console.WriteLine("Input character is {0}", ch);
//hit ENTER to exit the program
Console.ReadLine();
}
}
}
Output
输出量
First run:
Enter a character: H
Input character is H
Second run:
Enter a character: Hello world
Exception throws...
翻译自: https://www.includehelp.com/dot-net/methods-to-read-a-character-in-c-sharp.aspx
c#读取指定字符后的字符