Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

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

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 API is not CLS-compliant. 

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[CLSCompliantAttribute(false)]
public String(
	sbyte* value,
	int startIndex,
	int length,
	Encoding enc
)

Parameters

value
Type: System.SByte*

A pointer to an array of 8-bit signed integers.

startIndex
Type: System.Int32

The starting position within value.

length
Type: System.Int32

The number of characters within value to use.

enc
Type: System.Text.Encoding

An Encoding object that specifies how the array referenced by value is encoded. If enc is null, ANSI encoding is assumed.

ExceptionCondition
ArgumentNullException

value is null.

ArgumentOutOfRangeException

startIndex or length is less than zero.

-or-

The address specified by value + startIndex is too large for the current platform; that is, the address calculation overflowed.

-or-

The length of the new string to initialize is too large to allocate.

ArgumentException

The address specified by value + startIndex is less than 64K.

-or-

A new instance of String could not be initialized using value, assuming value is encoded as specified by enc.

AccessViolationException

value, startIndex, and length collectively specify an invalid address.

The value parameter is assumed to point to a signed byte array representing a string encoded as specified by the enc parameter. The enc parameter is an System.Text.Encoding object that represents any valid encoding method. It must represent the same method of encoding used to create the byte array or the decoded string will not necessarily be identical to the original string.

If length is zero, the new instance is initialized to the empty string ("").

This constructor processes characters from value starting at startIndex and ending at (startIndex + length - 1).

If the specified range is outside of the memory allocated for the sequence of characters, the behavior of this constructor is system dependent. For example, such a situation might cause an access violation.

In C#, this constructor is defined only in the context of unsafe code.

The following simple example demonstrates how you can create an instance of a UTF-8 String with this constructor.

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"
}

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

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.