C#中IndexOf和LastIndexOf方法如何使用
IndexOf和LastIndexOf方法都用于在字符串中查找特定字符或子字符串的位置。
IndexOf方法用于查找字符串中第一次出现特定字符或子字符串的位置,并返回该位置的索引值。如果未找到该字符或子字符串,则返回-1。
使用示例:
string str = "Hello, World!";
int index = str.IndexOf("o");
Console.WriteLine(index); // 输出结果为 4
LastIndexOf方法用于查找字符串中最后一次出现特定字符或子字符串的位置,并返回该位置的索引值。如果未找到该字符或子字符串,则返回-1。
使用示例:
string str = "Hello, World!";
int lastIndex = str.LastIndexOf("o");
Console.WriteLine(lastIndex); // 输出结果为 8
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:使用Caffe进行深度学习需要哪些基本步骤 下一篇:怎么配置一个Hadoop集群
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。