当前位置: 首页>>代码示例>>C#>>正文


C# String.Substring方法代码示例

本文整理汇总了C#中System.String.Substring方法的典型用法代码示例。如果您正苦于以下问题:C# String.Substring方法的具体用法?C# String.Substring怎么用?C# String.Substring使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.String的用法示例。


在下文中一共展示了String.Substring方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: foreach

string [] info = { "Name: Felica Walker", "Title: Mz.", 
                   "Age: 47", "Location: Paris", "Gender: F"};
int found = 0;

Console.WriteLine("The initial values in the array are:");
foreach (string s in info)
    Console.WriteLine(s);

Console.WriteLine("\nWe want to retrieve only the key information. That is:");        
foreach (string s in info) 
{
    found = s.IndexOf(": ");
    Console.WriteLine("   {0}", s.Substring(found + 2));
}
开发者ID:.NET开发者,项目名称:System,代码行数:14,代码来源:String.Substring

输出:

The initial values in the array are:
Name: Felica Walker
Title: Mz.
Age: 47
Location: Paris
Gender: F

We want to retrieve only the key information. That is:
Felica Walker
Mz.
47
Paris
F

示例2: foreach

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));
}
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:String.Substring

输出:

Key: Color1, Value: 'red'
Key: Color2, Value: 'green'
Key: Color3, Value: 'blue'
Key: Title, Value: 'Code Repository'

示例3:

String value = "This is a string.";
int startIndex = 5;
int length = 2;
String substring = value.Substring(startIndex, length);
Console.WriteLine(substring);
开发者ID:.NET开发者,项目名称:System,代码行数:5,代码来源:String.Substring

输出:

is

示例4:

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);
}
开发者ID:.NET开发者,项目名称:System,代码行数:14,代码来源:String.Substring

输出:

True
True
Index and length must refer to a location within the string.
Parameter name: length

示例5:

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));
开发者ID:.NET开发者,项目名称:System,代码行数:8,代码来源:String.Substring

输出:

aaaaabbbcccccccdd.Substring(5, 3) = bbb

示例6:

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);
开发者ID:.NET开发者,项目名称:System,代码行数:8,代码来源:String.Substring

输出:

Original string: extantstill in existence
Substring;       still in existence


注:本文中的System.String.Substring方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。