博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#读取指定字符后的字符_在C#中读取字符的不同方法
阅读量:2530 次
发布时间:2019-05-11

本文共 6386 字,大约阅读时间需要 21 分钟。

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:

可以使用以下方法来读取字符

  1. Using Console.ReadLine()[0]

    使用Console.ReadLine()[0]

  2. Using Console.ReadKey().KeyChar

    使用Console.ReadKey()。KeyChar

  3. Using Char.TryParse()

    使用Char.TryParse()

  4. 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: HInput character is HSecond run:Enter a character: Hello worldInput 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().KeyCharusing 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: Aresult is: TrueInput character is ASecond run:Enter a character: Hello worldresult is: FalseInput 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: HInput character is HSecond run: Enter a character: Hello worldException throws...

翻译自:

c#读取指定字符后的字符

转载地址:http://uyxzd.baihongyu.com/

你可能感兴趣的文章
spring第二冲刺阶段第七天
查看>>
搜索框键盘抬起事件2
查看>>
阿里百川SDK初始化失败 错误码是203
查看>>
透析Java本质-谁创建了对象,this是什么
查看>>
BFS和DFS的java实现
查看>>
QQ是怎样实现好友桌面快捷方式的?
查看>>
hadoop之安装
查看>>
Xamarin.Android开发实践(六)
查看>>
物联网项目的思考
查看>>
IntelliJ IDEA设置不自动打开最后关闭的项目
查看>>
抽象类
查看>>
ARC 101E.Ribbons on Tree(容斥 DP 树形背包)
查看>>
List数据分页
查看>>
kmp总结
查看>>
vuex模块相互调用
查看>>
js数字格式化千分位格式
查看>>
windows pip 安装 whl文件
查看>>
Java——常用类(String)
查看>>
Map容器
查看>>
tomcat查看线程数
查看>>