Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# lastindexof

作者:古枕清风   发布日期:2025-06-27   浏览:63

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 方法查找 substringtext 中最后出现的位置,并将结果存储在 lastIndex 变量中。
  • 最后,根据 lastIndex 的值输出相应的消息。

上一篇:c# interlocked

下一篇:c# 开发

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c#高并发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站