Export (0) Print
Expand All

String Constructor (SByte*, Int32, Int32, Encoding)

.NET Framework 1.1

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. For more information about CLS compliance, see What is the Common Language Specification.

[C#]
[CLSCompliant(false)]
unsafe public String(
 sbyte* value,
 int startIndex,
 int length,
 Encoding enc
);
[C++]
[CLSCompliant(false)]
public: String(
 char* value,
 int startIndex,
 int length,
 Encoding* enc
);

[Visual Basic] This constructor cannot be used in Visual Basic.

[JScript] This constructor cannot be used in JScript.

Parameters [C#, C++]

value
A pointer to an array of 8-bit signed integers.
startIndex
The starting position within value.
length
The number of characters within value to use.
enc
An Encoding object that specifies how the array referenced by value is encoded.

Exceptions [C#, C++]

Exception Type Condition
ArgumentOutOfRangeException startIndex or length is less than zero.
ArgumentException value + startIndex is less than 64K.

-or-

A new instance of String could not be initialized using value and the current code page.

Remarks [C#, C++]

If value is a null pointer or length is zero, the new instance is initialized to the empty string ("").

Example

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

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

Requirements [C#, C++]

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

See Also

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

Show:
© 2015 Microsoft