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
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

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

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

  1. python查找指定字符所在行号_python查找字符串中某个字符

    本文收集整理关于python查找字符串中某个字符的相关议题,使用内容导航快速到达. 内容导航: Q1:Python里统计一个字符串中另一个字符串的个数 答案为3(用正则):1234>>&g ...

  2. unicode字符大全可复制_说说Excel不可见字符的那些事

    今天小伙伴问了个问题 看上去啥也没有,为什么黏贴到记事本上前面那么多空白呢? 典型的不可见字符惹出来的麻烦,这个往往是公司软件导出数据造成的 我们今天就来细说说不可见字符的那些事 拿上面的例子说明 大 ...

  3. python如何增加字符串_在python中增加字符的方法

    在本教程中,我们将看到在Python中增加字符的不同方法. 类型转换 首先让我们看看如果在不进行类型转换的情况下向char添加int会发生什么. 示例## str initialization cha ...

  4. python删除字符串中重复字符_删除字符串中重复字符python 用CAD怎么画DNA反向

    用CAD怎么画DNA反向平行双螺旋结构绘螺旋线时,用选扭曲,确定顺时针. 画双头螺旋线时,第二根螺旋线底圆起点与第一根螺旋线底圆起点,可用角度分隔如180°.python去除文本中重复的字符串可有可无 ...

  5. java读取同包文件_Java实现从jar包中读取指定文件的方法

    本文实例讲述了Java实现从jar包中读取指定文件的方法.分享给大家供大家参考,具体如下: 以下的Java代码实现了从一个jar包中读取指定文件的功能: /** * This class implem ...

  6. c++读取txt文件中的数字_在Python中读取包中的数据文件的三种方式

    我们知道,写Python代码的时候,如果一个包(package)里面的一个模块要导入另一个模块,那么我们可以使用相对导入: 假设当前代码结构如下图所示: 其中test_1是一个包,在util.py里面 ...

  7. 用jsp_servlet实现在mysql中存储图片_从数据库中读取并生成图片的Servlet

    从数据库中读取并生成图片的Servlet 作者:未知    文章来源:www.jspcn.net 发布日期:2005年01月19日 作者:邵望 日期:2000-12-24 21:44:55 大体思路 ...

  8. python读取日期_从文件中读取日期和数据(Python)

    我想从文件中读取时间字符串和数据,但是当我使用loadtxt时,我不能同时读取字符串和数字,因为字符串不是浮点型的.所以我尝试使用genfromtxt并使用delimiter=[]+[]+[]作为我所 ...

  9. java 从excel中读取数据_在Java中读取Excel文件的内容和导出数据到Excel文件中

    转自www.chianjavaworld.net 原作者:SonyMusic 读:rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr 在Java ...

  10. java中读取excel数据类型_在Java中读取Excel文件的内容

    利用JExcelApi来动态生成excel文档 首先,请到http://www.andykhan.com/jexcelapi/index.html下载java excel api,主页上同时有比较详细 ...

最新文章

  1. apache开启.htaccess
  2. public,private,protected访问权限在Java,C++中的解析
  3. 利用阿里云自定义镜像实现服务器数据/网站快速迁移
  4. 开发高性能JAVA应用程序基础(内存篇)
  5. python坐牢-为什么说炒股要保护好本金 ?
  6. 服务器性能和活动监视
  7. java中paint_java中paint()的具体用法是什么?
  8. ES6新特性_ES6模板字符串---JavaScript_ECMAScript_ES6-ES11新特性工作笔记007
  9. 【Python】函数式编程
  10. 计算机网络 —— 组网
  11. L1、L2正则化区别和数学原理,以及什么是Elastic Net(弹性网络)正则项
  12. vscode配置c++11
  13. 蓝桥杯单片机——“”彩灯控制器”的程序设计
  14. HTML5中多媒体标签之音频标签
  15. php 菱形问号,python爬虫出现菱形问号乱码的解决方法
  16. 在Ubuntu16.04下配置VSFTPD
  17. Android 图片资源
  18. 比Linken Sphere(林肯法球)更多更新浏览器指纹的国产防关联工具-VMLogin中文版
  19. 每日一篇系列---CSS3实现下雨动效
  20. 花2万块买的教程!Android技术功底不够如何去面试,大厂直通车!

热门文章

  1. c语言不定方程的二元一次,poj1061 - 同余方程,二元一次不定方程
  2. 实验报告: 线性表的基本操作及应用
  3. java 对象复制字段_利用Java反射机制实现对象相同字段的复制
  4. 太阳能板清洗机器人科沃斯_太阳能电池板清洁机器人
  5. unity 彩带粒子_iOS动画开发----粒子系统---彩带效果
  6. #2002 - 服务器没有响应 (or the local MySQL server's socket is not ...
  7. UVA - 1589 ​​​​​​​Xiangqi
  8. 权限组件(6):权限分配的角色管理
  9. HTTP1.1新增了五种请求方法:OPTIONS、PUT、PATCH、DELETE、TRACE 、 CONNECT
  10. Eclipse安装试用Hanlp