String.Substring 方法 (Int32, Int32)

来源:互联网 发布:数据集中存储的好处 编辑:程序博客网 时间:2024/05/21 00:15

String.Substring 方法 (Int32, Int32)

.NET Framework 4.5
其他版本
1(共 1)对本文的评价是有帮助 评价此主题

从此实例检索子字符串。 子字符串从指定的字符位置开始且具有指定的长度。

命名空间:  System
程序集:  mscorlib(在 mscorlib.dll 中)

语法

C#
C++
F#
VB
public string Substring(int startIndex,int length)

参数

startIndex
类型:System.Int32
此实例中子字符串的起始字符位置(从零开始)。
length
类型:System.Int32
子字符串中的字符数。

返回值

类型:System.String
与此实例中在 startIndex 处开头、长度为 length 的子字符串等效的一个字符串,如果 startIndex 等于此实例的长度且 length 为零,则为 Empty

异常

异常条件ArgumentOutOfRangeException

startIndex 加 length 之和指示的位置不在此实例中。

- 或 -

startIndex 或 length 小于零。

备注

startIndex is zero-based.

说明说明

This method does not modify the value of the current instance. Instead, it returns a new string with length characters starting from the startIndexposition in the current string.

示例

The following example uses the Substring method in three cases to isolate substrings within a string. In two cases the substrings are used in comparisons, and in the third case an exception is thrown because invalid parameters are specified.

C#
VB
String myString = "abc";bool test1 = myString.Substring(2, 1).Equals("c"); // This is true.Console.WriteLine(test1);bool test2 = String.IsNullOrEmpty(myString.Substring(3, 0)); // This is true.Console.WriteLine(test2);try {   string str3 = myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.   Console.WriteLine(str3);}catch (ArgumentOutOfRangeException e) {   Console.WriteLine(e.Message);}         

版本信息

.NET Framework

受以下版本支持:4.5、4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

可移植类库

受以下版本支持:可移植类库

适用于 Windows 应用商店应用的 .NET

受以下版本支持:Windows 8

平台

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(支持带 SP1 或更高版本的服务器核心角色;不支持 Itanium)

并不是.NET Framework 对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求.

请参阅

参考

String类
Substring 重载
System 命名空间
Int32
Concat
Insert
Join
Remove
Replace
Split
Trim

社区附加资源

添加
0 0