Export (0) Print
Expand All

String Constructor (Char[], Int32, Int32)

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
)
public String (
	char[] value, 
	int startIndex, 
	int length
)
public function String (
	value : char[], 
	startIndex : int, 
	length : int
)

Parameters

value

An array of Unicode characters.

startIndex

The starting position within value.

length

The number of characters within value to use.

Exception typeCondition

ArgumentNullException

value is a 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.

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 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 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 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.Clone());

// Examine the result
Console.WriteLine(szGreekLetters);

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

Console.WriteLine("The Greek letter Alpha first appears at index " + ialpha +
    " and Omega last appears at index " + iomega + " in this String.");

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft