String.Substring Method (Int32)
.NET Framework 2.0
Retrieves a substring from this instance. The substring starts at a specified character position.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
The following code example demonstrates obtaining a substring from a string.
using System; public class SubStringTest { public static void Main() { 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("{0}We want to retrieve only the key information. That is:", Environment.NewLine); foreach (string s in info) { found = s.IndexOf(":"); Console.WriteLine(s.Substring(found + 1).Trim()); } } }
import System.*;
public class SubStringTest
{
public static void main(String[] args)
{
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:");
for (int iCtr = 0; iCtr < info.get_Length(); iCtr++) {
String s = info[iCtr];
Console.WriteLine(s);
}
Console.WriteLine("{0}We want to retrieve only the key information."
+ " That is:", Environment.get_NewLine());
for (int iCtr = 0; iCtr < info.get_Length(); iCtr++) {
String s = info[iCtr];
found = s.IndexOf(":");
Console.WriteLine(s.Substring(found + 1).Trim());
}
} //main
} //SubStringTest
import System; public class SubStringTest { public static function Main() : void { var info : String [] = ["Name: Felica Walker", "Title: Mz.", "Age: 47", "Location: Paris", "Gender: F"]; var found : int = 0; Console.WriteLine("The initial values in the array are:"); for (var i : int in info) Console.WriteLine(info[i]); Console.WriteLine("{0}We want to retrieve only the key information. That is:", Environment.NewLine); for (i in info) { found = info[i].IndexOf(":"); Console.WriteLine(info[i].Substring(found + 1).Trim()); } } } SubStringTest.Main();
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.