c#读取指定字符后的字符_在C#中读取字符的不同方法

本文介绍了在C#中读取单个字符的四种方法:使用Console.ReadLine()[0],Console.ReadKey().KeyChar,Char.TryParse()和Convert.ToChar()。详细解释了每个方法的工作原理和示例代码。
摘要由CSDN通过智能技术生成

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];

示例ÿ
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值