using System;
class Program
{
    static void Main()
    {
        string text = "Hello, welcome to the world of C#!";
        string substring = "o";
        // 使用 LastIndexOf 方法查找子字符串最后出现的位置
        int lastIndex = text.LastIndexOf(substring);
        if (lastIndex != -1)
        {
            Console.WriteLine($"The last occurrence of '{substring}' is at index: {lastIndex}");
        }
        else
        {
            Console.WriteLine($"The substring '{substring}' was not found.");
        }
    }
}LastIndexOf 方法用于查找指定子字符串在当前字符串中最后一次出现的位置。它返回一个整数值,表示子字符串的索引位置。如果未找到子字符串,则返回 -1。text 和一个要查找的子字符串 substring。LastIndexOf 方法查找 substring 在 text 中最后出现的位置,并将结果存储在 lastIndex 变量中。lastIndex 的值输出相应的消息。上一篇:c# interlocked
下一篇:c# 开发
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站