Windows Dev Center

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

Parameters

startIndex

The 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 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.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show:
© 2015 Microsoft