Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.Substring Method (Int32)

Retrieves a substring from this instance. The substring starts at a specified character position.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public string Substring (
	int startIndex
)
public String Substring (
	int startIndex
)
public function Substring (
	startIndex : int
) : String
Not applicable.

Parameters

startIndex

The zero-based starting character position of a substring in this instance.

Return Value

A String object equivalent to the substring that begins at startIndex in this instance, or Empty if startIndex is equal to the length of this instance.

Exception typeCondition

ArgumentOutOfRangeException

startIndex is less than zero or greater than the length of this instance.

The index is zero-based.

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 Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2014 Microsoft