C# String Substring(Int32, Int32)

Description

String Substring(Int32, Int32) retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

Syntax

String.Substring(Int32, Int32) has the following syntax.


public string Substring(
  int startIndex,
  int length
)

Parameters

String.Substring(Int32, Int32) has the following parameters.

  • startIndex - The zero-based starting character position of a substring in this instance.
  • length - The number of characters in the substring.

Returns

String.Substring(Int32, Int32) method returns A string that is equivalent to the substring of length length that begins at startIndex in this instance, or Empty if startIndex is equal to the length of this instance and length is zero.

Example

The following example uses the Substring method in three cases to isolate substrings within a string.


/*from   w w w .j a  v a 2 s. com*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
       String myString = "this is a test";
       string str3 = myString.Substring(3, 1); 
       Console.WriteLine(str3);
  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version