一、indexOf()

indexOf("\\"):返回"\\"字符在此实例中第一个出现的索引位置,实例的下标是从0开始,如果未找到则返回-1.

indexOf("\\", 7):返回在此实例中从下标7开始的,第一次出现"\\"的位置,如果未找到返回-1.

二、lastIndexOf()

lastIndexOf("\\"):返回"\\"在此实例中最后一个出现的索引位置。即从右向左搜索,第一次出现的"\\"的位置,如果未找到则返回-1.

lastIndexOf("\\", 7):返回在此实例中从下标0开始到下标7结束的这一段子串中,最后一次出现"\\"的位置 。即从右向左搜索,第一次出现的"/"的位置,如果未找到则返回-1.

三、subString()

Substring:截取字符串。Substring(7,2)表示从下标7开始,截取长度为2的字符串,Substring(7)表示从下标7开始,一直截取到字符串末尾。

PS:indexOf和lastIndexOf的区别就搜索的方向不一样,indexOf是从左向右,lastIndexOf是从右向左,尽管搜索方向不一样,但是字符下标依然从左向右加1,从0开始。

四、例子:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string FilePath = "D:\\files\\small.txt";
Console.WriteLine(FilePath); int a= FilePath.IndexOf("f");
Console.WriteLine(a); int b = FilePath.IndexOf("s",);
Console.WriteLine(b); int c = FilePath.LastIndexOf("x");
Console.WriteLine(c); int d = FilePath.LastIndexOf("s",);
Console.WriteLine(d); string e = FilePath.Substring(,);
Console.WriteLine(e); string f = FilePath.Substring();
Console.WriteLine(f); int index = FilePath.LastIndexOf('\\');
Console.WriteLine(index);
string folder = FilePath.Substring(, index);
Console.WriteLine(folder);
string ShapeName = FilePath.Substring(index + );
Console.WriteLine(ShapeName); Console.ReadKey();
}
}
}

运行结果:

最新文章

  1. java中文乱码解决之道(一)-----认识字符集
  2. docker4dotnet #4 使用Azure云存储构建高速 Docker registry
  3. windows和linux中搭建python集成开发环境IDE——如何设置多个python环境
  4. Wintel物联网平台-Windows IoT新手入门指南
  5. 推荐20个很有帮助的 Web 前端开发教程
  6. 【NDK开发】android-ndk r10环境搭建
  7. 用ConfigParser模块读写配置文件——Python
  8. 使用Linq 来解决Datatable 去除数据重复
  9. Valera and Tubes
  10. [leetcode-573-Squirrel Simulation]
  11. ATS 分级缓存
  12. 输入url到渲染出页面的过程
  13. [SDOI2009]虔诚的墓主人
  14. IntelliJ IDEA添加jar包
  15. nodejs 动态创建二维码
  16. Java集合实现
  17. apache加载模块的说明
  18. hive进行词频统计
  19. Linux上的oracle巡检脚本
  20. 20171113xlVba指定文件夹多簿多表分表合并150

热门文章

  1. HDU 6521 K-th Closest Distance (主席树+二分)
  2. python笔记21(面向对象课程三)
  3. 安装ik分词插件
  4. php 全局变量 预定义变量
  5. finalize的用法
  6. kubernetes集合
  7. Github搜索技巧
  8. MySQL命令随手记之alter
  9. C# 如何实现完整的INI文件读写类
  10. 共同战“疫”,CODING 帮助研发团队高效协同