C# 属性

C# 抽象

C# 字符串

C# 泛型

C# 杂项

C# 新特性

C# LastIndexOf() 最后一个索引字符串

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

签名

句法 (Syntax)

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() 方法示例

例子 (Example)

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() 与 LastIndexOf() 示例

IndexOf() 方法返回第一个匹配字符的索引号,而 LastIndexOf() 方法返回最后一个匹配字符的索引号。

例子 (Example)

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


  • 使用社交账号登录,本站支持
全部评论(0)