Strings.GetChar(String, Int32) Method

Definition

Returns a Char value representing the character from the specified index in the supplied string.

C#
public static char GetChar(string str, int Index);

Parameters

str
String

Required. Any valid String expression.

Index
Int32

Required. Integer expression. The (1-based) index of the character in str to be returned.

Returns

Char value representing the character from the specified index in the supplied string.

Exceptions

str is Nothing, Index < 1, or Index is greater than index of last character of str.

Examples

This example shows how to use the GetChar function to return a character from a specified index in a String.

VB
Dim testString As String = "ABCDE"
Dim testChar As Char
' Returns "D"
testChar = GetChar(testString, 4)

Remarks

If Index is less than 1 or greater than the index of the last character in str, an ArgumentException is thrown.

Applies to

Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also