C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

C# String LastIndexOf()

C# String LastIndexOf()

C#LastIndexOf() 方法用于查找指定字符在 String 中最后一次出现的索引位置。

签名

public int LastIndexOf(Char ch)
public int LastIndexOf(Char, Int32)
public int LastIndexOf(Char, Int32, Int32)
public int LastIndexOf(String)
public int LastIndexOf(String, Int32)
public int LastIndexOf(String, Int32, Int32)
public int LastIndexOf(String, Int32, Int32, StringComparison)
public int LastIndexOf(String, Int32, StringComparison)
public int LastIndexOf(String, StringComparison)

参数

ch:是一个字符类型参数,用于查找字符串中给定字符的最后一次出现。

返回

返回整数值。

C# String LastIndexOf() 方法示例

using System; 
    public class StringExample  
    {  
        public static void Main(string[] args)  
        {  
           string s1 = "Hello C#";
           int index = s1.LastIndexOf('l');
           Console.WriteLine(index);
      }  
    }  
输出:
3

C# String IndexOf() vs LastIndexOf() 示例

IndexOf() 方法返回第一个匹配字符的索引号,而 LastIndexOf() 方法返回第一个匹配字符的索引号最后匹配的字符。
using System; 
    public class StringExample  
    {  
        public static void Main(string[] args)  
        {  
           string s1 = "Hello C#";
           int first = s1.IndexOf('l');
           int last = s1.LastIndexOf('l');
           Console.WriteLine(first);
           Console.WriteLine(last);
      }  
    }  
输出:
2
3
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4