String Constructor (array<Char>[]()[], Int32, Int32)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Initializes a new instance of the String class to the value indicated by an array of Unicode characters, a starting character position within that array, and a length.

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

Syntax

Public Sub New ( _
    value As Char(), _
    startIndex As Integer, _
    length As Integer _
)
public String(
    char[] value,
    int startIndex,
    int length
)

Parameters

Exceptions

Exception Condition
ArgumentNullException

value is nullNothingnullptra null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

startIndex or length is less than zero.

-or-

The sum of startIndex and length is greater than the number of elements in value.

Remarks

If length is zero, an Empty instance is initialized.

This constructor copies Unicode characters from value, starting at startIndex and ending at (startIndex + length - 1).

Performance Considerations

Applications that parse or decode streams of text often use the String constructor or StringBuilder..::.Append(array<Char>[]()[], Int32, Int32) method to convert runs of characters into a string. If your application repeatedly encounters the same runs of characters, it will waste memory if it repeatedly creates new strings with the same value instead of creating and reusing one string.

If your application will repeatedly encounter the same runs of characters, but you cannot predict what those runs will be, you might consider using a lookup table instead of creating new String objects with the String constructor. For example, suppose your application reads and parses a stream of characters from a file that contains XML tags and attributes. When you parse the stream, you repeatedly encounter certain tokens (that is, runs of characters with symbolic meaning). Tokens equivalent to the strings "0", "1", "true", and "false" are likely to occur frequently in an XML stream.

Instead of converting each token your application encounters into a new string, you can create a NameTable object to hold commonly occurring strings. The NameTable object improves performance because it retrieves stored strings without allocating temporary memory.

When your application encounters a token, use the NameTable..::.Get(array<Char>[]()[], Int32, Int32) method to attempt to retrieve the token from the table. If the token exists, the method returns the corresponding string. If the token does not exist, use the NameTable..::.Add(array<Char>[]()[], Int32, Int32) method to insert the token into the table. After the token is inserted, the method returns the corresponding string. In either case, the appropriate string is returned to your application.

Examples

The following simple code example demonstrates how you can create an instance of the String class with this constructor.

' Create a Unicode String with 5 Greek Alpha characters
Dim szGreekAlpha As New String(ChrW(&H319), 5)
' Create a Unicode String with a Greek Omega character
Dim szGreekOmega As New String(New Char() {ChrW(&H3A9), ChrW(&H3A9), _
                                           ChrW(&H3A9)}, 2, 1)

Dim szGreekLetters As String = String.Concat(szGreekOmega, szGreekAlpha, _
                                             szGreekOmega)

' Examine the result
outputBlock.Text &= szGreekLetters & vbCrLf

' The first index of Alpha
Dim iAlpha As Integer = szGreekLetters.IndexOf(ChrW(&H319))
' The last index of Omega
Dim iomega As Integer = szGreekLetters.LastIndexOf(ChrW(&H3A9))

outputBlock.Text &= String.Format("The Greek letter Alpha first appears at index {0}.", _
                  iAlpha) & vbCrLf
outputBlock.Text &= String.Format("The Greek letter Omega last appears at index {0}.", _
                  iomega) & vbCrLf
// Create a Unicode String with 5 Greek Alpha characters
String szGreekAlpha = new String('\u0319', 5);
// Create a Unicode String with a Greek Omega character
String szGreekOmega = new String(new char[] { '\u03A9', '\u03A9', '\u03A9' }, 2, 1);

String szGreekLetters = String.Concat(szGreekOmega, szGreekAlpha, szGreekOmega);

// Examine the result
outputBlock.Text += szGreekLetters + "\n";

// The first index of Alpha
int ialpha = szGreekLetters.IndexOf('\u0319');
// The last index of Omega
int iomega = szGreekLetters.LastIndexOf('\u03A9');

outputBlock.Text += "The Greek letter Alpha first appears at index " + ialpha +
    " and Omega last appears at index " + iomega + " in this String." + "\n";

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

String Class

String Overload

System Namespace

Char

Int32