C#中String.LastIndexOf方法小結(jié)
返回指定 Unicode 字符或字符串在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。 如果未在此實(shí)例中找到該字符或字符串,則此方法返回 -1。
一、重載
LastIndexOf(String, Int32, Int32, StringComparison) | 報告指定字符串在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。 搜索在所指定的字符位置的數(shù)目的字符串開始時,開始指定字符和其后面的位置。 一個參數(shù)指定要執(zhí)行搜索指定字符串的比較類型。 |
LastIndexOf(String, Int32, StringComparison) | 報告指定字符串在當(dāng)前 String 對象中最后一個匹配項(xiàng)的從零開始的索引。 在指定的字符位置開始和在向后的右邊該字符串的開頭處理的搜索。 一個參數(shù)指定要執(zhí)行搜索指定字符串的比較類型。 |
LastIndexOf(String, Int32, Int32) | 報告指定字符串在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。 搜索在指定字符位置的數(shù)目的字符串開始時,開始指定字符和其后面的位置。 |
LastIndexOf(Char, Int32, Int32) | 報告指定的 Unicode 字符在此實(shí)例內(nèi)的子字符串中的最后一個匹配項(xiàng)的從零開始的索引的位置。 搜索在指定字符位置的數(shù)目的字符串開始時,開始指定字符和其后面的位置。 |
LastIndexOf(String, Int32) | 報告指定字符串在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。 在指定的字符位置開始和在向后的右邊該字符串的開頭處理的搜索。 |
LastIndexOf(Char, Int32) | 報告指定 Unicode 字符在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。 在指定的字符位置開始和在向后的右邊該字符串的開頭處理的搜索。 |
LastIndexOf(String) | 報告指定字符串在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。 |
LastIndexOf(Char) | 報告指定 Unicode 字符在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。 |
LastIndexOf(String, StringComparison) | 報告指定字符串在當(dāng)前 String 對象中最后一個匹配項(xiàng)的從零開始的索引。 一個參數(shù)指定要用于指定字符串的搜索類型。 |
二、LastIndexOf(String)
報告指定字符串在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。
public int LastIndexOf (string value); 參數(shù) value String 要搜尋的字符串。 返回 Int32 如果找到該字符串,則 value 為從零開始的起始索引位置;如果未找到該字符串,則為 -1。 例外 ArgumentNullException value 為 null。
三、LastIndexOf(Char)
報告指定 Unicode 字符在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。
1.定義
public int LastIndexOf (char value); 參數(shù) value Char 要查找的 Unicode 字符。 返回 Int32 如果找到該字符,則 value 為從零開始的索引位置;如果未找到,則為 -1。
2.示例
打開一個文件,并分離出文件路徑、文件名、文件擴(kuò)展名。
// Substring和LastIndexOf() // 分離文件路徑、文件名及擴(kuò)展名 namespace _043 { public partial class Form1 : Form { private Button? button1; private GroupBox? groupBox1; private Label? label3; private Label? label2; private Label? label1; private OpenFileDialog? openFileDialog1; public Form1() { InitializeComponent(); Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { openFileDialog1 = new OpenFileDialog { FileName = "openFileDialog1" }; // // button1 // button1 = new Button { Location = new Point(141, 12), Name = "button1", Size = new Size(75, 23), TabIndex = 0, Text = "打開文件", UseVisualStyleBackColor = true }; button1.Click += Button1_Click; // // label3 // label3 = new Label { AutoSize = true, Location = new Point(11, 85), Name = "label3", Size = new Size(56, 17), TabIndex = 2, Text = "擴(kuò)展名:" }; // // label2 // label2 = new Label { AutoSize = true, Location = new Point(11, 57), Name = "label2", Size = new Size(56, 17), TabIndex = 1, Text = "文件名:" }; // // label1 // label1 = new Label { AutoSize = true, Location = new Point(11, 29), Name = "label1", Size = new Size(68, 17), TabIndex = 0, Text = "文件路徑:" }; // // groupBox1 // groupBox1 = new GroupBox { Location = new Point(12, 41), Name = "groupBox1", Size = new Size(340, 118), TabIndex = 1, TabStop = false, Text = "文件路徑、文件名、擴(kuò)展名:" }; groupBox1.Controls.Add(label3); groupBox1.Controls.Add(label2); groupBox1.Controls.Add(label1); groupBox1.SuspendLayout(); // // Form1 // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(364, 171); Controls.Add(groupBox1); Controls.Add(button1); Name = "Form1"; StartPosition = FormStartPosition.CenterScreen; Text = "分離文件路徑、文件名、擴(kuò)展名"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); } private void Button1_Click(object? sender, EventArgs e) { if (openFileDialog1!.ShowDialog() == DialogResult.OK) //判斷是否選擇了文件 { string str_file = openFileDialog1.FileName; //記錄選擇的文件全路徑 string filepath = //獲取文件路徑 str_file[..(str_file.LastIndexOf('\\') + 1)]; string filename = //獲取文件名 str_file[(str_file.LastIndexOf('\\') + 1)..str_file.LastIndexOf('.')]; string file_extension = //獲取文件擴(kuò)展名 str_file.Substring(str_file.LastIndexOf('.') + 1, str_file.Length - str_file.LastIndexOf('.') - 1); label1!.Text = "文件路徑: " + filepath; //顯示文件路徑 label2!.Text = "文件名稱: " + filename; //顯示文件名 label3!.Text = "文件擴(kuò)展名: " + file_extension; //顯示擴(kuò)展名 } } } }
三、LastIndexOf(String, StringComparison)
報告指定字符串在當(dāng)前 String 對象中最后一個匹配項(xiàng)的從零開始的索引。 一個參數(shù)指定要用于指定字符串的搜索類型。
1.定義
public int LastIndexOf (string value, StringComparison comparisonType); 參數(shù) value String 要搜尋的字符串。 comparisonType StringComparison 指定搜索規(guī)則的枚舉值之一。 返回 Int32 如果找到該字符串,則value 為從零開始的起始索引位置;如果未找到該字符串,則為 -1。 例外 ArgumentNullException value 為 null。 ArgumentException comparisonType 不是有效的 StringComparison 值。
四、LastIndexOf(String, Int32, Int32, StringComparison)
報告指定字符串在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。 搜索在所指定的字符位置的數(shù)目的字符串開始時,開始指定字符和其后面的位置。 一個參數(shù)指定要執(zhí)行搜索指定字符串的比較類型。
public int LastIndexOf (string value, int startIndex, int count, StringComparison comparisonType); 參數(shù) value String 要搜尋的字符串。 startIndex Int32 搜索起始位置。 從此實(shí)例的 startIndex 開始搜索。 count Int32 要檢查的字符位置數(shù)。 comparisonType StringComparison 指定搜索規(guī)則的枚舉值之一。 返回 Int32 如果找到該字符串,則value為從零開始的起始索引位置;如果未找到該字符串或當(dāng)前實(shí)例等于 Empty,則為 -1。 例外 ArgumentNullException value 為 null。 ArgumentOutOfRangeException count 為負(fù)數(shù)。 或 當(dāng)前實(shí)例不等于 Empty,并且 startIndex 為負(fù)數(shù)。 或 當(dāng)前實(shí)例不等于 Empty,并且 startIndex 大于此實(shí)例的長度。 或 當(dāng)前實(shí)例不等于 Empty,并且 startIndex + 1 - count 指定不在此實(shí)例內(nèi)的位置。 或 當(dāng)前實(shí)例等于 Empty 并且 start 小于 -1 或大于零。 或 當(dāng)前實(shí)例等于 Empty 并且 count 大于 1。 ArgumentException comparisonType 不是有效的 StringComparison 值。
五、LastIndexOf(String, Int32, StringComparison)
報告指定字符串在當(dāng)前 String 對象中最后一個匹配項(xiàng)的從零開始的索引。 在指定的字符位置開始和在向后的右邊該字符串的開頭處理的搜索。 一個參數(shù)指定要執(zhí)行搜索指定字符串的比較類型。
public int LastIndexOf (string value, int startIndex, StringComparison comparisonType); 參數(shù) value String 要搜尋的字符串。 startIndex Int32 搜索起始位置。 從此實(shí)例的 startIndex 開始搜索。 comparisonType StringComparison 指定搜索規(guī)則的枚舉值之一。 返回 Int32 如果找到該字符串,則value為從零開始的起始索引位置;如果未找到該字符串或當(dāng)前實(shí)例等于 Empty,則為 -1。 例外 ArgumentNullException value 為 null。 ArgumentOutOfRangeException 當(dāng)前實(shí)例不等于 Empty,并且 startIndex 小于零或大于當(dāng)前實(shí)例的長度。 或 當(dāng)前實(shí)例等于 Empty,并且 startIndex 小于-1 或大于零。 ArgumentException comparisonType 不是有效的 StringComparison 值。
六、LastIndexOf(String, Int32, Int32)
報告指定字符串在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。 搜索在指定字符位置的數(shù)目的字符串開始時,開始指定字符和其后面的位置。
public int LastIndexOf (string value, int startIndex, int count); 參數(shù) value String 要搜尋的字符串。 startIndex Int32 搜索起始位置。 從此實(shí)例的 startIndex 開始搜索。 count Int32 要檢查的字符位置數(shù)。 返回 Int32 如果找到該字符串,則value為從零開始的起始索引位置;如果未找到該字符串或當(dāng)前實(shí)例等于 Empty,則為 -1。 例外 ArgumentNullException value 為 null。 ArgumentOutOfRangeException count 為負(fù)數(shù)。 或 當(dāng)前實(shí)例不等于 Empty,并且 startIndex 為負(fù)數(shù)。 或 當(dāng)前實(shí)例不等于 Empty,并且 startIndex 大于此實(shí)例的長度。 或 當(dāng)前實(shí)例不等于 Empty,并且 startIndex - count + 1 指定不在此實(shí)例內(nèi)的位置。 或 當(dāng)前實(shí)例等于 Empty 并且 start 小于 -1 或大于零。 或 當(dāng)前實(shí)例等于 Empty 并且 count 大于 1。
七、LastIndexOf(Char, Int32, Int32)
報告指定的 Unicode 字符在此實(shí)例內(nèi)的子字符串中的最后一個匹配項(xiàng)的從零開始的索引的位置。 搜索在指定字符位置的數(shù)目的字符串開始時,開始指定字符和其后面的位置。
public int LastIndexOf (char value, int startIndex, int count); 參數(shù) value Char 要查找的 Unicode 字符。 startIndex Int32 搜索的起始位置。 從此實(shí)例的startIndex開始搜索。 count Int32 要檢查的字符位置數(shù)。 返回 Int32 如果找到該字符,則value為從零開始的索引位置;如果未找到該字符或當(dāng)前實(shí)例等于 Empty,則為 -1。 例外 ArgumentOutOfRangeException 當(dāng)前實(shí)例不等于 Empty,并且 startIndex 小于零或大于等于當(dāng)前實(shí)例的長度。 或 當(dāng)前實(shí)例不等于 Empty,并且 startIndex - count + 1 小于零。
八、LastIndexOf(String, Int32)
報告指定字符串在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。 在指定的字符位置開始和在向后的右邊該字符串的開頭處理的搜索。
public int LastIndexOf (string value, int startIndex); 參數(shù) value String 要搜尋的字符串。 startIndex Int32 搜索起始位置。 從此實(shí)例的startIndex開始搜索。 返回 Int32 如果找到該字符串,則為 value 的從零開始的起始索引位置;如果未找到該字符串或當(dāng)前實(shí)例等于 Empty,則為 -1。 例外 ArgumentNullException value 為 null。 ArgumentOutOfRangeException 當(dāng)前實(shí)例不等于 Empty,并且 startIndex 小于零或大于當(dāng)前實(shí)例的長度。 或 當(dāng)前實(shí)例等于 Empty,并且 startIndex 小于-1 或大于零。
九、LastIndexOf(Char, Int32)
報告指定 Unicode 字符在此實(shí)例中的最后一個匹配項(xiàng)的從零開始的索引的位置。 在指定的字符位置開始和在向后的右邊該字符串的開頭處理的搜索。
1.定義
public int LastIndexOf (char value, int startIndex); 參數(shù) value Char 要查找的 Unicode 字符。 startIndex Int32 搜索的起始位置。 從此實(shí)例的startIndex開始搜索。 返回 Int32 如果找到該字符,則value為從零開始的索引位置;如果未找到該字符或當(dāng)前實(shí)例等于 Empty,則為 -1。 例外 ArgumentOutOfRangeException 當(dāng)前實(shí)例不等于 Empty,并且 startIndex 小于零或大于等于當(dāng)前實(shí)例的長度。
2.示例
// Sample for String.LastIndexOf(Char, Int32) namespace LastIndexOf { class Sample { public static void Main() { string br1 = "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"; string br2 = "0123456789012345678901234567890123456789012345678901234567890123456"; string str = "Now is the time for all good men to come to the aid of their party."; int start; int at; start = str.Length - 1; Console.WriteLine("All occurrences of 't' from position {0} to 0.", start); Console.WriteLine("{1}{0}{2}{0}{3}{0}", Environment.NewLine, br1, br2, str); Console.Write("The letter 't' occurs at position(s): "); at = 0; while (start > -1 && at > -1) { at = str.LastIndexOf('t', start); if (at > -1) { Console.Write("{0} ", at); start = at - 1; } } Console.Write("{0}{0}{0}", Environment.NewLine); //3個空行 } } } /* 運(yùn)行結(jié)果: All occurrences of 't' from position 66 to 0. 0----+----1----+----2----+----3----+----4----+----5----+----6----+- 0123456789012345678901234567890123456789012345678901234567890123456 Now is the time for all good men to come to the aid of their party. The letter 't' occurs at position(s): 64 55 44 41 33 11 7 */
到此這篇關(guān)于C#中String.LastIndexOf方法小結(jié)的文章就介紹到這了,更多相關(guān)C# String.LastIndexOf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用TimeSpan時間計算的簡單實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于C#使用TimeSpan時間計算的相關(guān)資料,以及通過一個實(shí)例代碼給大家介紹了C#使用timespan和timer完成一個簡單的倒計時器的方法,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06使用C#實(shí)現(xiàn)Windows組和用戶管理的示例代碼
這篇文章主要介紹了使用C#實(shí)現(xiàn)Windows組和用戶管理的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實(shí)現(xiàn)
本文主要介紹了C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07提高C# StringBuilder操作性能優(yōu)化的方法
本篇文章主要介紹使用C# StringBuilder 的項(xiàng)目實(shí)踐,用于減少內(nèi)存分配,提高字符串操作的性能。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11C# winform點(diǎn)擊生成二維碼實(shí)例代碼
這篇文章主要介紹了 C# winform點(diǎn)擊生成二維碼實(shí)例代碼,需要的朋友可以參考下2017-04-04C#實(shí)現(xiàn)變量交換、斐波那契數(shù)列、質(zhì)數(shù)、回文方法合集
這篇文章介紹了C#實(shí)現(xiàn)變量交換、斐波那契數(shù)列、質(zhì)數(shù)、回文的方法合集,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02C# 16進(jìn)制與字符串、字節(jié)數(shù)組之間的轉(zhuǎn)換
在串口通訊過程中,經(jīng)常要用到 16進(jìn)制與字符串、字節(jié)數(shù)組之間的轉(zhuǎn)換2009-05-05C#實(shí)現(xiàn)泛型List分組輸出元素的方法
這篇文章主要介紹了C#實(shí)現(xiàn)泛型List分組輸出元素的方法,涉及C#針對List的遍歷、排序、輸出等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12