C#實現(xiàn)字符串倒序遍歷的方法小結(jié)
1.使用Reverse()方法
實現(xiàn)字符串的倒序遍歷,可以使用Enumerable.Reverse<TSource>(IEnumerable<TSource>) 方法。
(1)定義
反轉(zhuǎn)序列中元素的順序。
using System.Linq; public static System.Collections.Generic.IEnumerable<TSource> Reverse<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
類型參數(shù)
TSource source 的元素類型。
參數(shù)
source IEnumerable<TSource> 要反轉(zhuǎn)的值序列。
返回
IEnumerable<TSource> 一個序列,其元素以相反順序?qū)?yīng)于輸入序列的元素。
例如
ArgumentNullException
source 為 null。
(2)示例
// 使用Reverse()方法給字符串倒序遍歷 namespace _122_1 { internal class Program { private static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); Console.WriteLine(new string("Hello, World!".Reverse().ToArray())); } } } //運行結(jié)果: /* !dlroW ,olleH */
2.使用for循環(huán)
// 使用for循環(huán)給字符串倒序遍歷 namespace _122_2 { internal class Program { private static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); string input = "Hello, World!"; string reversed = ""; for (int i = input.Length - 1; i >= 0; i--) { reversed += input[i]; } Console.WriteLine(reversed); } } } //運行結(jié)果: /* !dlroW ,olleH */
3.使用IEnumerable()迭代器
使用迭代器實現(xiàn)C#字符串的倒序遍歷,可以使用yield關(guān)鍵字創(chuàng)建一個迭代器函數(shù)。
(1)示例1
示例創(chuàng)建了一個名為ReverseIterator的迭代器函數(shù)。然后,使用for循環(huán)從后向前訪問字符串中的字符,并使用yield return語句返回每個字符。在Main函數(shù)中,使用string.Join方法將迭代器中的字符連接成一個字符串,輸出倒序的字符串:!dlroW ,olleH。
// 使用迭代器給字符串倒序遍歷 namespace _122_3 { class Program { static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); string input = "Hello, World!"; string reversed = string.Join("", ReverseIterator(input)); Console.WriteLine(reversed); } static IEnumerable<char> ReverseIterator(string input) { for (int i = input.Length - 1; i >= 0; i--) { yield return input[i]; } } } } //運行結(jié)果: /* !dlroW ,olleH */
(2)示例2
// 使用迭代器實現(xiàn)倒序遍歷 namespace _122 { public partial class Form1 : Form { private Label? label1; private Label? label2; private TextBox? textBox1; private TextBox? textBox2; private Button? button1; public Form1() { InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { // // label1 // label1 = new Label { AutoSize = true, Location = new Point(12, 11), Name = "label1", Size = new Size(68, 17), TabIndex = 0, Text = "源字符串:" }; // // label2 // label2 = new Label { AutoSize = true, Location = new Point(12, 40), Name = "label2", Size = new Size(68, 17), TabIndex = 1, Text = "倒排結(jié)果:" }; // // textBox1 // textBox1 = new TextBox { Location = new Point(77, 8), Name = "textBox1", Size = new Size(165, 23), TabIndex = 2 }; // // textBox2 // textBox2 = new TextBox { Location = new Point(77, 34), Name = "textBox2", Size = new Size(165, 23), TabIndex = 3 }; // // button1 // button1 = new Button { Location = new Point(167, 61), Name = "button1", Size = new Size(75, 23), TabIndex = 4, Text = "倒序排列", UseVisualStyleBackColor = true }; button1.Click += Button1_Click; // // Form1 // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(254, 96); Controls.Add(button1); Controls.Add(textBox2); Controls.Add(textBox1); Controls.Add(label2); Controls.Add(label1); Name = "Form1"; Text = "用迭代器實現(xiàn)倒序遍歷"; } private void Button1_Click(object? sender, EventArgs e) { textBox2!.Clear(); textBox2.Text = GetValue(textBox1!.Text); } /// <summary> /// 通過迭代器實現(xiàn)字符串的倒序 /// 從末尾開始遍歷字符串 /// </summary> /// <param string="n">進行倒序的字符串</param> /// <returns>以對象的方式倒序返回單個字符</returns> public static IEnumerable<object> Transpose(string n) { if (n.Length > 0) //如果泛型不為空 { for (int i = n.Length - 1; i >= 0; i--) yield return n[i]; //返回數(shù)據(jù)集合 } } /// <summary> /// 獲取倒序后的字符串靜態(tài)方法 /// </summary> /// <param string="str">進行倒序的字符串</param> /// <returns>返回倒序后的字符串</returns> public static string GetValue(string str) { if (str.Length == 0) //判斷字符串長度是否為0 return ""; string temp_str = ""; //記錄倒序之后的字符串 foreach (object i in Transpose(str)) temp_str += i.ToString();//訪問接口并獲取迭代器中的每個字符 return temp_str; //返回倒序之后的字符串 } } }
4.使用List<T>
使用List<T>來實現(xiàn)字符串的倒序遍歷,可以先將字符串轉(zhuǎn)換為字符列表,然后使用Reverse方法對列表進行反轉(zhuǎn),最后遍歷列表以生成倒序字符串。
示例首先將輸入字符串轉(zhuǎn)換為字符列表,然后使用Reverse方法對列表進行反轉(zhuǎn)。接著,我們使用foreach循環(huán)遍歷反轉(zhuǎn)后的字符列表,并將每個字符添加到倒序字符串中。最后,輸出倒序字符串:!dlroW ,olleH。
// 使用List<T>給字符串倒序遍歷 namespace _122_4 { class Program { static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); string input = "Hello, World!"; List<char> charList = new(input); charList.Reverse(); string reversed = ""; foreach (char c in charList) { reversed += c; } Console.WriteLine(reversed); } } } //運行結(jié)果: /* !dlroW ,olleH */
5.使用List<T>的迭代器
使用List<T>和迭代器來實現(xiàn)字符串的倒序遍歷,可以先將字符串轉(zhuǎn)換為字符列表,然后使用迭代器函數(shù)從后向前訪問字符并返回。
示例首先將輸入字符串轉(zhuǎn)換為字符列表。然后,創(chuàng)建了一個名為ReverseIterator的迭代器函數(shù),它接受字符列表作為輸入。使用for循環(huán)從后向前訪問字符列表中的字符,并使用yield return語句返回每個字符。在Main函數(shù)中,使用string.Join方法將迭代器中的字符連接成一個字符串,輸出倒序的字符串:!dlroW ,olleH。
// 使用List<T>的迭代器給字符串倒序遍歷 namespace _122_5 { class Program { static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); string input = "Hello, World!"; List<char> charList = new(input); //charList.Reverse(); string reversed = string.Join("", ReverseIterator(charList)); Console.WriteLine(reversed); } static IEnumerable<char> ReverseIterator(List<char> charList) { for (int i = charList.Count - 1; i >= 0; i--) { yield return charList[i]; } } } } //運行結(jié)果: /* !dlroW ,olleH */
6.使用IList<T>
使用IList<T>來實現(xiàn)字符串的倒序遍歷,可以先將字符串轉(zhuǎn)換為字符列表,然后使用Reverse方法對列表進行反轉(zhuǎn),最后遍歷列表以生成倒序字符串。
示例首先將輸入字符串轉(zhuǎn)換為字符列表,并使用Reverse方法對列表進行反轉(zhuǎn)。然后,我們使用for循環(huán)從后向前訪問字符列表中的字符,并將每個字符添加到倒序字符串中。最后,輸出倒序字符串:!dlroW ,olleH。
在這個示例中,使用IList<T>接口而不是List<T>類,這意味著可以使用任何實現(xiàn)了IList<T>接口的類,例如ArrayList。
// 使用IList<T>給字符串倒序遍歷 namespace _122_6 { class Program { static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); string input = "Hello, World!"; IList<char> charList = new List<char>(input); //charList!.Reverse(); string reversed = ""; for (int i = charList.Count - 1; i >= 0; i--) { reversed += charList[i]; } Console.WriteLine(reversed); } } } //運行結(jié)果: /* !dlroW ,olleH */
7.使用IList<T>的迭代器
使用IList<T>和迭代器來實現(xiàn)字符串的倒序遍歷,先將字符串轉(zhuǎn)換為字符列表,然后使用迭代器函數(shù)從后向前訪問字符并返回。
示例首先將輸入字符串轉(zhuǎn)換為字符列表。然后,創(chuàng)建了一個名為ReverseIterator的迭代器函數(shù),它接受一個字符列表作為輸入。使用for循環(huán)從后向前訪問字符列表中的字符,并使用yield return語句返回每個字符。在Main函數(shù)中,我們使用string.Join方法將迭代器中的字符連接成一個字符串,輸出倒序的字符串:!dlroW ,olleH。
在這個示例中,使用IList<T>接口而不是List<T>類,這意味著可以使用任何實現(xiàn)了IList<T>接口的類,例如ArrayList。
// 使用IList<T>的迭代器給字符串倒序遍歷 namespace _122_7 { class Program { static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); string input = "Hello, World!"; IList<char> charList = new List<char>(input); //charList.Reverse(); string reversed = string.Join("", ReverseIterator(charList)); Console.WriteLine(reversed); } static IEnumerable<char> ReverseIterator(IList<char> charList) { for (int i = charList.Count - 1; i >= 0; i--) { yield return charList[i]; } } } } //運行結(jié)果: /* !dlroW ,olleH */
到此這篇關(guān)于C#實現(xiàn)字符串倒序遍歷的方法小結(jié)的文章就介紹到這了,更多相關(guān)C#字符串倒序遍歷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中把FastReport.Net報表控件的數(shù)據(jù)保存到數(shù)據(jù)庫
這篇文章介紹了在數(shù)據(jù)庫中保存FastReport.Net報表的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06利用C#與PLC通信實現(xiàn)設(shè)備遠程控制與管理
PLC是工業(yè)自動化中用于控制機械設(shè)備、生產(chǎn)線等的核心設(shè)備,通過與PLC的通信,我們可以實現(xiàn)設(shè)備的遠程監(jiān)控、數(shù)據(jù)采集等功能,C#作為一種現(xiàn)代化的編程語言,能夠非常方便地與PLC進行通信,本文將介紹如何利用C#與PLC進行通信,并實現(xiàn)設(shè)備的遠程控制與管理2025-02-02C#實現(xiàn)統(tǒng)計字數(shù)功能的方法
這篇文章主要介紹了C#實現(xiàn)統(tǒng)計字數(shù)功能的方法,較為詳細的分析了C#字數(shù)統(tǒng)計功能的原理與實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08C#實現(xiàn)矩陣加法、取負、數(shù)乘、乘法的方法
這篇文章主要介紹了C#實現(xiàn)矩陣加法、取負、數(shù)乘、乘法的方法,涉及C#針對矩陣的數(shù)學(xué)運算相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2015-08-08C#中FormClosing與FormClosed的區(qū)別詳細解析
本文是對C#中FormClosing與FormClosed的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10