C#實(shí)現(xiàn)字符串倒序遍歷的方法小結(jié)
1.使用Reverse()方法
實(shí)現(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> 一個(gè)序列,其元素以相反順序?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()));
}
}
}
//運(yùn)行結(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);
}
}
}
//運(yùn)行結(jié)果:
/*
!dlroW ,olleH
*/3.使用IEnumerable()迭代器
使用迭代器實(shí)現(xiàn)C#字符串的倒序遍歷,可以使用yield關(guān)鍵字創(chuàng)建一個(gè)迭代器函數(shù)。
(1)示例1
示例創(chuàng)建了一個(gè)名為ReverseIterator的迭代器函數(shù)。然后,使用for循環(huán)從后向前訪問字符串中的字符,并使用yield return語句返回每個(gè)字符。在Main函數(shù)中,使用string.Join方法將迭代器中的字符連接成一個(gè)字符串,輸出倒序的字符串:!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];
}
}
}
}
//運(yùn)行結(jié)果:
/*
!dlroW ,olleH
*/(2)示例2
// 使用迭代器實(shí)現(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 = "用迭代器實(shí)現(xiàn)倒序遍歷";
}
private void Button1_Click(object? sender, EventArgs e)
{
textBox2!.Clear();
textBox2.Text = GetValue(textBox1!.Text);
}
/// <summary>
/// 通過迭代器實(shí)現(xiàn)字符串的倒序
/// 從末尾開始遍歷字符串
/// </summary>
/// <param string="n">進(jìn)行倒序的字符串</param>
/// <returns>以對(duì)象的方式倒序返回單個(gè)字符</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]; //返回?cái)?shù)據(jù)集合
}
}
/// <summary>
/// 獲取倒序后的字符串靜態(tài)方法
/// </summary>
/// <param string="str">進(jìn)行倒序的字符串</param>
/// <returns>返回倒序后的字符串</returns>
public static string GetValue(string str)
{
if (str.Length == 0) //判斷字符串長(zhǎng)度是否為0
return "";
string temp_str = ""; //記錄倒序之后的字符串
foreach (object i in Transpose(str))
temp_str += i.ToString();//訪問接口并獲取迭代器中的每個(gè)字符
return temp_str; //返回倒序之后的字符串
}
}
}
4.使用List<T>
使用List<T>來實(shí)現(xiàn)字符串的倒序遍歷,可以先將字符串轉(zhuǎn)換為字符列表,然后使用Reverse方法對(duì)列表進(jìn)行反轉(zhuǎn),最后遍歷列表以生成倒序字符串。
示例首先將輸入字符串轉(zhuǎn)換為字符列表,然后使用Reverse方法對(duì)列表進(jìn)行反轉(zhuǎn)。接著,我們使用foreach循環(huán)遍歷反轉(zhuǎn)后的字符列表,并將每個(gè)字符添加到倒序字符串中。最后,輸出倒序字符串:!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);
}
}
}
//運(yùn)行結(jié)果:
/*
!dlroW ,olleH
*/5.使用List<T>的迭代器
使用List<T>和迭代器來實(shí)現(xiàn)字符串的倒序遍歷,可以先將字符串轉(zhuǎn)換為字符列表,然后使用迭代器函數(shù)從后向前訪問字符并返回。
示例首先將輸入字符串轉(zhuǎn)換為字符列表。然后,創(chuàng)建了一個(gè)名為ReverseIterator的迭代器函數(shù),它接受字符列表作為輸入。使用for循環(huán)從后向前訪問字符列表中的字符,并使用yield return語句返回每個(gè)字符。在Main函數(shù)中,使用string.Join方法將迭代器中的字符連接成一個(gè)字符串,輸出倒序的字符串:!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];
}
}
}
}
//運(yùn)行結(jié)果:
/*
!dlroW ,olleH
*/6.使用IList<T>
使用IList<T>來實(shí)現(xiàn)字符串的倒序遍歷,可以先將字符串轉(zhuǎn)換為字符列表,然后使用Reverse方法對(duì)列表進(jìn)行反轉(zhuǎn),最后遍歷列表以生成倒序字符串。
示例首先將輸入字符串轉(zhuǎn)換為字符列表,并使用Reverse方法對(duì)列表進(jìn)行反轉(zhuǎn)。然后,我們使用for循環(huán)從后向前訪問字符列表中的字符,并將每個(gè)字符添加到倒序字符串中。最后,輸出倒序字符串:!dlroW ,olleH。
在這個(gè)示例中,使用IList<T>接口而不是List<T>類,這意味著可以使用任何實(shí)現(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);
}
}
}
//運(yùn)行結(jié)果:
/*
!dlroW ,olleH
*/7.使用IList<T>的迭代器
使用IList<T>和迭代器來實(shí)現(xiàn)字符串的倒序遍歷,先將字符串轉(zhuǎn)換為字符列表,然后使用迭代器函數(shù)從后向前訪問字符并返回。
示例首先將輸入字符串轉(zhuǎn)換為字符列表。然后,創(chuàng)建了一個(gè)名為ReverseIterator的迭代器函數(shù),它接受一個(gè)字符列表作為輸入。使用for循環(huán)從后向前訪問字符列表中的字符,并使用yield return語句返回每個(gè)字符。在Main函數(shù)中,我們使用string.Join方法將迭代器中的字符連接成一個(gè)字符串,輸出倒序的字符串:!dlroW ,olleH。
在這個(gè)示例中,使用IList<T>接口而不是List<T>類,這意味著可以使用任何實(shí)現(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];
}
}
}
}
//運(yùn)行結(jié)果:
/*
!dlroW ,olleH
*/到此這篇關(guān)于C#實(shí)現(xiàn)字符串倒序遍歷的方法小結(jié)的文章就介紹到這了,更多相關(guān)C#字符串倒序遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#多線程學(xué)習(xí)之(五)使用定時(shí)器進(jìn)行多線程的自動(dòng)管理
這篇文章主要介紹了C#多線程學(xué)習(xí)之使用定時(shí)器進(jìn)行多線程的自動(dòng)管理,實(shí)例分析了C#使用timer定時(shí)器類實(shí)現(xiàn)針對(duì)多線程的自動(dòng)管理功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
DevExpress實(shí)現(xiàn)TreeList向上遞歸獲取符合條件的父節(jié)點(diǎn)
這篇文章主要介紹了DevExpress實(shí)現(xiàn)TreeList向上遞歸獲取符合條件的父節(jié)點(diǎn),需要的朋友可以參考下2014-08-08
使用C#與設(shè)備接口進(jìn)行無縫通信的實(shí)現(xiàn)技巧
隨著物聯(lián)網(wǎng)、智能設(shè)備和自動(dòng)化技術(shù)的快速發(fā)展,越來越多的設(shè)備需要與計(jì)算機(jī)系統(tǒng)進(jìn)行實(shí)時(shí)通信,而C#作為一門強(qiáng)大的編程語言,憑借其廣泛的庫支持和高效的開發(fā)效率,已成為與設(shè)備接口對(duì)接的理想選擇,在本篇文章中,我們將探討如何使用C#與設(shè)備進(jìn)行無縫通信2025-01-01
C#?內(nèi)存安全性的重大演進(jìn):重新定義?unsafe?關(guān)鍵字
本文揭示了C#與.NETL1的內(nèi)存安全變革,通過精準(zhǔn)定位危險(xiǎn)區(qū)域,讓C#更接近Rust的安全保障,引入newalloc三條件、safe關(guān)鍵字上下文等新模型代號(hào)MemorySafetyRules,同時(shí)提供ref安全的逃生通道,助力開發(fā)者在新舊模型間平穩(wěn)過渡,感興趣的朋友跟隨小編一起看看吧2026-06-06
C#實(shí)現(xiàn)Word轉(zhuǎn)換RTF的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)Word轉(zhuǎn)換RTF,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
利用多線程句柄設(shè)置鼠標(biāo)忙碌狀態(tài)的實(shí)現(xiàn)方法
怎樣利用多線程句柄設(shè)置鼠標(biāo)忙碌狀態(tài)呢?下面小編就為大家介紹一下具體的實(shí)現(xiàn)方法吧!需要的朋友可以過來參考下2013-08-08

