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

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: 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#读取指定字符后的字符

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543007.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用python 对图片进行水印,保护自己写的文章

1,关于文章被爬 说起来挺桑心的,好不容易写的文章,被爬走。 用个搜索引擎搜索都不是在第一位,写的文章全给这些网站提供流量了。 这种网站还居多广告。 还是抱怨少点吧。csdn对于这些事情也是无所作为啊。 最起码的防盗链也不…

r语言descstats_一条命令轻松绘制CNS顶级配图-ggpubr

Hadley Wickham创建的可视化包ggplot2可以流畅地进行优美的可视化,但是如果要通过ggplot2定制一套图形,尤其是适用于杂志期刊等出版物的图形,对于那些没有深入了解ggplot2的人来说就有点困难了,ggplot2的部分语法是很晦涩的。为此…

android layout_width 属性,android:layout_weight属性详解

在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示。android并没用提供table这样的控件,虽然有TableLayout,但是它并非是我们想象中的像html里面的t…

angular的$http发送post,get请求无法传送参数的问题

2019独角兽企业重金招聘Python工程师标准>>> 用$http进行异步请求的时候发现了一个奇怪的事情,用$http.post(url,data)的方法进行请求,后台死活接收不到data的参数,真是百思不得姐啊..... 折腾了老半天才在stackoverflow上找到答案…

python变量和常量_Python数学模块常量和示例

python变量和常量Python数学模块常量 (Python math module constants) In the math module, there are some of the defined constants that can be used for various mathematical operations, these are the mathematical constants and returns their values equivalent to …

怎样解决Word文档图标无法正常显示的问题?

此类问题是由于 Word 程序相关组件损坏导致,可以通过下面的方案来解决:步骤/方法按键盘上的 Windows 徽标健 R 键,输入 regedit,按回车键。(若弹出用户账户控制窗口,请允许以继续)对于 Word 200…

android 对话框的父view是谁,android – 在对话框中获取相对于其父级的视图位置

我想要做的是,从按钮边缘到屏幕上的一点画一条线……我正在使用一个对话框片段…我尝试的所有函数总是返回0点…我试过以下:Overrideprotected Dialog createDialog(Bundle savedInstanceState, FragmentActivity activity){Dialog d builder.create();View v Lay…

np.radians_带有Python示例的math.radians()方法

np.radiansPython math.radians()方法 (Python math.radians() method) math.radians() method is a library method of math module, it is used to convert angle value from degree to radians, it accepts a number and returns the angle value in radians. math.radians(…

怎么用git将本地代码上传到远程服务器_git之如何把本地文件上传到远程仓库的指定位置...

2018.11.26添加内容:对于自己的仓库,我们建议将远程仓库通过clone命令把整个仓库克隆到本地的某一路径下。这样的话我们从本地向远程仓库提交代码时,就可以直接把需要提交的文件拖到我们之前克隆下来的路径下,接下来在这整个仓库下…

MathType与Origin是怎么兼容的

MathType作为一款常用的公式编辑器,可以与很多的软件兼容使用。Origin虽然是一款专业绘图与数据分析软件,但是在使用过程中也是可以用到MathType。它可以帮助Origin给图表加上标签,或者在表格中增加公式标签。但是一些用户朋友对这个不能不是…

c语言 函数的参数传递示例_llround()函数以及C ++中的示例

c语言 函数的参数传递示例C llround()函数 (C llround() function) llround() function is a library function of cmath header, it is used to round the given value and casts to a long long integer, it accepts a number and returns the integer (long long int) valu…

android requestmtu,android - 如何设置/获取/请求从Android到iOS或反之亦然BLE的MTU? - 堆栈内存溢出...

我们正在将MTU请求从Android发送到iOSAndroid-从此函数onServicesDiscovered回调请求MTU但是我不知道如何确定对等设备支持是否请求了MTU,以及如何实际协商的MTU。 仅在API级别22(Android L 5.1)中添加了必需的函数:BluetoothGattCallback.onMtuChanged(…

AutoBookmark Adobe Acrobat快速自动批量添加书签/目录

前言 解决问题:Adobe Acrobat快速自动批量添加书签/目录, 彻底告别手动添加书签的烦恼 AutoBookmark 前言1 功能简介2 实现步骤2.1 下载插件2.2 将插件复制到Acrobat文件夹下2.3 自动生成书签 1 功能简介 我们在查看PDF版本的论文或者其他文件的时候, 虽然相比较于…

Python调用微博API获取微博内容

一:获取app-key 和 app-secret 使用自己的微博账号登录微博开放平台(http://open.weibo.com/),在微博开放中心下“创建应用”创建一个应用,应用信息那些随便填,填写完毕后,不需要提交审核,需要的只是那个ap…

python独立log示例_带有Python示例的math.log1p()方法

python独立log示例Python math.log1p()方法 (Python math.log1p() method) math.log1p() method is a library method of math module, it is used to get the natural logarithm of 1x (base e), it accepts a number and returns the natural logarithm of 1number on base e…

15947884 oracle_Oracle Patch Bundle Update

一、相关知识介绍以前只知道有CPU(Critical Patch Update)和PSU(Patch Set Update),不知道还有个Bundle Patch,由于出现了TNS-12531的BUG问题,需要在windows上打至少为Patch bundle 22补丁。通过学习查找:Oracle里的补丁具体分为如下这样6种类型&#xf…

鸿蒙系统hdc,HDC2020有看头:要揭开鸿蒙系统和EMUI11神秘面纱?

IFA2020算是HDC2020的预热吧,一个是9月2日在德国柏林举办的消费电子展,一个是在松山湖举办的华为开发者大会,二者的目的都一样,但也有一丝不同,IFA是为了让老外了解HMS、了解华为的智慧生态,而HDC2020就是要…

UVA 12501 Bulky process of bulk reduction ——(线段树成段更新)

和普通的线段树不同的是,查询x~y的话,给出的答案是第一个值的一倍加上第二个值的两倍一直到第n个值的n倍。 思路的话,就是关于query和pushup的方法。用一个新的变量sum记录一下这个区间里面按照答案给出的方式的值,比如说&#xf…

gdb ldexp_带有Python示例的math.ldexp()方法

gdb ldexpPython math.ldexp()方法 (Python math.ldexp() method) math.ldexp() method is a library method of math module, it is used to calculate expression x*(2**i), where x is a mantissa and i is an exponent. It accepts two numbers (x is either float or inte…

windows安装包删了会有影响吗_win7系统删除系统更新安装包的详细教程

win7系统使用久了,好多网友反馈说win7系统删除系统更新安装包的问题,非常不方便。有什么办法可以永久解决win7系统删除系统更新安装包的问题,面对win7系统删除系统更新安装包的图文步骤非常简单,只需要1.其实在win7旗舰版系统中&a…