日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2025/3/11 C# 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:

可以使用以下方法來讀取字符 :

  • Using Console.ReadLine()[0]

    使用Console.ReadLine()[0]

  • Using Console.ReadKey().KeyChar

    使用Console.ReadKey()。KeyChar

  • Using Char.TryParse()

    使用Char.TryParse()

  • 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 characterConsole.WriteLine("Input character is {0}", ch);//hit ENTER to exit the programConsole.ReadLine();}} }

    Output

    輸出量

    First run: Enter a character: H Input character is HSecond 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 characterConsole.WriteLine("Input character is {0}", ch);//hit ENTER to exit the programConsole.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 characterConsole.WriteLine("result is: {0}", result);Console.WriteLine("Input character is {0}", ch);//hit ENTER to exit the programConsole.ReadLine();}} }

    Output

    輸出量

    First run: Enter a character: A result is: True Input character is ASecond 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 characterConsole.WriteLine("Input character is {0}", ch);//hit ENTER to exit the programConsole.ReadLine();}} }

    Output

    輸出量

    First run: Enter a character: H Input character is HSecond 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#中读取字符的不同方法的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。