Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12300024
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类:

2012-11-06 09:43:05

一、理论及例程


String substring(int beginIndex) 
String substring(int beginIndex, int endIndex) 
String.Substring (Int32)         子字符串从指定的字符位置开始。 
String.Substring (Int32, Int32) 子字符串从指定的字符位置开始且具有指定的长度。


  1. 举例如下:
  2.              string s = "Hello C# World!";
  3.              //s1为从s中截取的位置为3的字符以后的字符子串,3表示子字符串的起始字符位置
  4. string s1=s.Substring(3);
  5.              //s2为从s中截取的位置为6的字符开始长度为2的字符串,6表示子字符的起始字符位置,2表示子字符长度
  6. string s2 = s.Substring(6, 2);
  7. 结果如下:
  8. lo C#
  9. C#

 

二、实践案例

1、截取字符串最后一个截取符向前的字符串


  1. string s = Globle_One_Conf_Dir;
  2. s = s.Substring(0, s.LastIndexOf("\\"));

其中 

Globle_One_Conf_Dir = "F:\\软件工程开发目录\\1111111111111111111159\\设计文档.xml"

 

截取所得

s = "F:\\软件工程开发目录\\1111111111111111111159"

 

 

2、截取字符串最后一个截取符向后的字符串


  1. string s = Globle_One_Conf_Dir;
  2. s = s.Substring(s.LastIndexOf("\\"));


其中

Globle_One_Conf_Dir = "F:\\软件工程开发目录\\1111111111111111111159\\设计文档.xml"

 

截取所得

s = "\\报到205.xml"

阅读(1652) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~