Export (0) Print
Expand All

String Constructor

.NET Framework 1.1

Initializes a new instance of the String class.

Overload List

[C#, C++] Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters. This constructor is not CLS-compliant.

Supported by the .NET Compact Framework.

[C#] unsafe public String(char*);
[C++] public: String(__wchar_t*);

Initializes a new instance of the String class to the value indicated by an array of Unicode characters.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Char())
[C#] public String(char[]);
[C++] public: String(__wchar_t __gc[]);
[JScript] public function String(Char[]);

[C#, C++] Initializes a new instance of the String class to the value indicated by a pointer to an array of 8-bit signed integers. This constructor is not CLS-compliant.

Supported by the .NET Compact Framework.

[C#] unsafe public String(sbyte*);
[C++] public: String(char*);

Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Char, Integer)
[C#] public String(char, int);
[C++] public: String(__wchar_t, int);
[JScript] public function String(Char, int);

[C#, C++] Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters, a starting character position within that array, and a length. This constructor is not CLS-compliant.

Supported by the .NET Compact Framework.

[C#] unsafe public String(char*, int, int);
[C++] public: String(__wchar_t*, int, int);

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.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Char(), Integer, Integer)
[C#] public String(char[], int, int);
[C++] public: String(__wchar_t __gc[], int, int);
[JScript] public function String(Char[], int, int);

[C#, C++] Initializes a new instance of the String class to the value indicated by a specified pointer to an array of 8-bit signed integers, a starting character position within that array, and a length. This constructor is not CLS-compliant.

[C#] unsafe public String(sbyte*, int, int);
[C++] public: String(char*, int, int);

[C#, C++] Initializes a new instance of the String class to the value indicated by a specified pointer to an array of 8-bit signed integers, a starting character position within that array, a length, and an Encoding object. This constructor is not CLS-compliant.

[C#] unsafe public String(sbyte*, int, int, Encoding);
[C++] public: String(char*, int, int, Encoding*);

Example

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

[C#, C++] Note   This example shows how to use one of the overloaded versions of the String constructor. For other examples that might be available, see the individual overload topics.
[C#] 
unsafe
{
    String utfeightstring = null;
    sbyte [] asciiChars = new sbyte[] { 0x51,0x52,0x53,0x54,0x54,0x56 };
    UTF8Encoding encoding = new UTF8Encoding(true, true);

    // Instruct the Garbage Collector not to move the memory
    fixed(sbyte* pAsciiChars = asciiChars)
    {
        utfeightstring = new String(pAsciiChars,0,asciiChars.Length,encoding);
    }
    Console.WriteLine("The UTF8 String is " + utfeightstring ); // prints "QRSTTV"
}

[C++] 
char asciiChars [6] =  { 0x51,0x52,0x53,0x54,0x54,0x56 };
char* pstr6 = &asciiChars[0];
UTF8Encoding* encoding = new UTF8Encoding(true, true);
String* utfeightstring = new String(pstr6,0,sizeof(asciiChars),encoding);

// prints "QRSTTV"
Console::WriteLine(String::Concat("The UTF8 String is ", utfeightstring));

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

See Also

String Class | String Members | System Namespace

Show:
© 2014 Microsoft