Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 1.1
.NET Framework
Reference
System
String Class
Methods
Substring Method
 Substring Method (Int32)

  Switch on low bandwidth view
.NET Framework Class Library
String.Substring Method (Int32)

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

[Visual Basic]
Overloads Public Function Substring( _
   ByVal startIndex As Integer _
) As String
[C#]
public string Substring(
 int startIndex
);
[C++]
public: String* Substring(
 int startIndex
);
[JScript]
public function Substring(
   startIndex : int
) : String;

Parameters

startIndex
The starting character position of a substring in this instance.

Return Value

A String equivalent to the substring that begins at startIndex in this instance.

-or-

Empty if startIndex is equal to the length of this instance.

Exceptions

Exception Type Condition
ArgumentOutOfRangeException startIndex is less than zero or greater than the length of this instance.

Remarks

The index is zero-based.

Example

The following example demonstrates obtaining a substring from a string.

[Visual Basic] 
Imports System

Public Class SubStringTest
    
    Public Shared Sub Main()
        Dim info As String() = {"Name: Felica Walker", "Title: Mz.", "Age: 47", "Location: Paris", "Gender: F"}
        Dim found As Integer = 0
       
        Console.WriteLine("The initial values in the array are:")
        Dim s As String
        For Each s In  info
            Console.WriteLine(s)
        
        Next s
        Console.WriteLine("{0}We want to retrieve only the key information. That is:", Environment.NewLine)
        
        For Each s In  info
            found = s.IndexOf(":")
            Console.WriteLine(s.Substring((found + 1)).Trim())
        Next s
    End Sub 'Main
End Class 'SubStringTest

[C#] 
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());
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;

int main()
{
   String* info[] = {S"Name: Felica Walker", S"Title: Mz.", S"Age: 47", S"Location: Paris", S"Gender: F"};
   int found = 0;

   Console::WriteLine(S"The initial values in the array are:");
   IEnumerator* myEnum1 = info->GetEnumerator();
   while (myEnum1->MoveNext()) {
      String* s = __try_cast<String*>(myEnum1->Current);
      Console::WriteLine(s);
   }
   Console::WriteLine(S"\nWe want to retrieve only the key information. That is:");        
   IEnumerator* myEnum2 = info->GetEnumerator();
   while (myEnum2->MoveNext()) {
      String* s = __try_cast<String*>(myEnum2->Current);
      found = s->IndexOf(S":");
      Console::WriteLine(s->Substring(found + 1)->Trim());
   }
}

[JScript] 
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();

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | String.Substring Overload List | Int32 | Concat | Insert | Join | Remove | Replace | Split | Trim

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker