最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)字符串倒序遍歷的方法小結(jié)

 更新時(shí)間:2024年02月18日 08:31:47   作者:wenchm  
這篇文章主要為大家詳細(xì)介紹了C#中實(shí)現(xiàn)字符串倒序遍歷的常見方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價(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)管理

    這篇文章主要介紹了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)

    這篇文章主要介紹了DevExpress實(shí)現(xiàn)TreeList向上遞歸獲取符合條件的父節(jié)點(diǎn),需要的朋友可以參考下
    2014-08-08
  • 使用C#與設(shè)備接口進(jìn)行無縫通信的實(shí)現(xiàn)技巧

    使用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
  • .Net Winform開發(fā)筆記(一)

    .Net Winform開發(fā)筆記(一)

    理解“Windows 窗體應(yīng)用程序”項(xiàng)目中Program.cs文件中的main方法與傳統(tǒng)C++Console控制臺(tái)程序中的main方法的區(qū)別等等,感興趣的朋友可以了解下
    2013-01-01
  • C#實(shí)現(xiàn)自定義雙擊事件

    C#實(shí)現(xiàn)自定義雙擊事件

    這篇文章主要介紹了C#實(shí)現(xiàn)自定義雙擊事件,需要的朋友可以參考下
    2014-08-08
  • C# 動(dòng)態(tài)加載程序集信息

    C# 動(dòng)態(tài)加載程序集信息

    在設(shè)計(jì)模式的策略模式中,需要?jiǎng)討B(tài)加載程序集信息,本文通過一個(gè)簡(jiǎn)單的實(shí)例,來講解動(dòng)態(tài)加載Dll需要的知識(shí)點(diǎn)。下面跟著小編一起來看下吧
    2017-03-03
  • C#?內(nèi)存安全性的重大演進(jìn):重新定義?unsafe?關(guān)鍵字

    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的示例代碼

    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)方法

    怎樣利用多線程句柄設(shè)置鼠標(biāo)忙碌狀態(tài)呢?下面小編就為大家介紹一下具體的實(shí)現(xiàn)方法吧!需要的朋友可以過來參考下
    2013-08-08
  • C# WINFORM自定義異常處理方法

    C# WINFORM自定義異常處理方法

    這篇文章主要介紹了一個(gè)簡(jiǎn)單的統(tǒng)一異常處理方法。系統(tǒng)底層出現(xiàn)異常,寫入記錄文件,系統(tǒng)頂層捕獲底層異常,顯示提示信息。需要的可以參考一下
    2021-12-12

最新評(píng)論

乐山市| 临高县| 玉龙| 许昌市| 石台县| 佳木斯市| 高要市| 伽师县| 正蓝旗| 常山县| 静安区| 长寿区| 鄂尔多斯市| 白朗县| 泰顺县| 玛曲县| 马尔康县| 习水县| 天等县| 酒泉市| 青铜峡市| 潍坊市| 广灵县| 前郭尔| 高尔夫| 昔阳县| 合江县| 汤原县| 特克斯县| 铜山县| 南靖县| 来安县| 高青县| 汶川县| 常山县| 东兴市| 绥化市| 武平县| 长沙县| 信宜市| 红安县|