String Constructor (Char[], Int32, Int32)

May 11, 2014

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)

public String(
	char[] value,
	int startIndex,
	int length
)

Parameters

value
Type: System.Char []
An array of Unicode characters.
startIndex
Type: System.Int32
The starting position within value.
length
Type: System.Int32
The number of characters within value to use.

ExceptionCondition
ArgumentNullException

value is null.

ArgumentOutOfRangeException

startIndex or length is less than zero.

-or-

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

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

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
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";


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft