Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

String Constructor (Char[], Int32, Int32)

.NET Framework 1.1

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.

[Visual Basic]
Public Sub New( _
   ByVal value() As Char, _
   ByVal startIndex As Integer, _
   ByVal length As Integer _
)
[C#]
public String(
   char[] value,
 int startIndex,
 int length
);
[C++]
public: String(
   __wchar_t value __gc[],
 int startIndex,
 int length
);
[JScript]
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.

Exceptions

Exception Type Condition
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.

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

Example

[C#, C++] The following simple code example demonstrates how you can create an instance of the String class with this constructor.

[C#] 
// 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.");

[C++] 
// Create a Unicode String with 5 Greek Alpha characters
String* szGreekAlpha = new String(L'\x0319',5);
// Create a Unicode String with a Greek Omega character
wchar_t charArray5 [3] = {L'\x03A9',L'\x03A9',L'\x03A9'};
String* szGreekOmega = new String( charArray5 ,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(L'\x0319');
// The last index of Omega
int iomega = szGreekLetters->LastIndexOf(L'\x03A9');

Console::WriteLine(String::Concat("The Greek letter Alpha first appears at index ",Convert::ToString(ialpha) ));
Console::WriteLine(String::Concat(" and Omega last appears at index ", Convert::ToString(iomega), " in this String."));

[Visual Basic, JScript] No example is available for Visual Basic or JScript. To view a C# or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

String Class | String Members | System Namespace | String Constructor Overload List | Char | Int32

Show:
© 2015 Microsoft