Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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
)

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 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('\u0391',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('\u0391');
// 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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.