本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
|
翻译
英语
|
String.Substring 方法 (Int32, Int32)
发布日期: 2016年7月
程序集: mscorlib(位于 mscorlib.dll)
返回值
Type: System.StringException | Condition |
---|---|
ArgumentOutOfRangeException |
![]() |
---|
如果已为单个字符,是标记的末尾的子字符串,搜索length参数等于endIndex- startIndex + 1,其中endIndex是返回值的IndexOf或IndexOf方法。 下面的示例从字符串提取"b"字符的连续块。 using System; public class Example { public static void Main() { String s = "aaaaabbbcccccccdd"; Char charRange = 'b'; int startIndex = s.IndexOf(charRange); int endIndex = s.LastIndexOf(charRange); int length = endIndex - startIndex + 1; Console.WriteLine("{0}.Substring({1}, {2}) = {3}", s, startIndex, length, s.Substring(startIndex, length)); } } // The example displays the following output: // aaaaabbbcccccccdd.Substring(5, 3) = bbb
如果您已搜索多个字符的标记的末尾的子字符串,length参数等于endIndex+ endMatchLength - startIndex,其中endIndex是返回值的IndexOf或IndexOf方法。 和endMatchLength是标记的子字符串末尾的字符序列的长度。 下面的示例提取包含 XML 的文本块<definition>元素。 using System; public class Example { public static void Main() { String s = "<term>extant<definition>still in existence</definition></term>"; String searchString = "<definition>"; int startIndex = s.IndexOf(searchString); searchString = "</" + searchString.Substring(1); int endIndex = s.IndexOf(searchString); String substring = s.Substring(startIndex, endIndex + searchString.Length - startIndex); Console.WriteLine("Original string: {0}", s); Console.WriteLine("Substring; {0}", substring); } } // The example displays the following output: // Original string: <term>extant<definition>still in existence</definition></term> // Substring; <definition>still in existence</definition>
如果字符或字符序列不包含的末尾的子字符串,在length参数等于endIndex- startIndex,其中endIndex是返回值的IndexOf或IndexOf方法。
它提取单个字符和字符串 (在索引 2) 中的第三个位置,并将其替换为"c"进行比较。 此比较返回true。 它提取开始 (索引 3) 处的字符串中的第四个位置零字符并将其传递给IsNullOrEmpty方法。 这返回 true,因为调用Substring方法返回String.Empty。 它尝试提取一个字符在字符串中的第四个位置启动。 由于该位置没有任何字符,方法调用将引发ArgumentOutOfRangeException异常。
using System; public class Sample { public static void Main() { 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); } } } // The example displays the following output: // True // True // Index and length must refer to a location within the string. // Parameter name: length
using System; public class Example { public static void Main() { String[] pairs = { "Color1=red", "Color2=green", "Color3=blue", "Title=Code Repository" }; foreach (var pair in pairs) { int position = pair.IndexOf("="); if (position < 0) continue; Console.WriteLine("Key: {0}, Value: '{1}'", pair.Substring(0, position), pair.Substring(position + 1)); } } } // The example displays the following output: // Key: Color1, Value: 'red' // Key: Color2, Value: 'green' // Key: Color3, Value: 'blue' // Key: Title, Value: 'Code Repository'
自 8 起可用
.NET Framework
自 1.1 起可用
可移植类库
在 可移植 .NET 平台 中受支持
Silverlight
自 2.0 起可用
Windows Phone Silverlight
自 7.0 起可用
Windows Phone
自 8.1 起可用
The M:System.String.IndexOf(System.String) method is used to get the position of the equals character in the string.. The call to the M:System.String.Substring(System.Int32,System.Int32) method extracts the key name., which starts from the first character in the string and extends for the number of characters returned by the call to the M:System.String.IndexOf(System.String) method. The call to the M:System.String.Substring(System.Int32) method then extracts the value assigned to the key. It starts at one character position beyond the equals character and extends to the end of the string.