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.pswp.cn/news/543007.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/543007.shtml
英文地址,請注明出處:http://en.pswp.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…