.NET Framework Class Library
String Constructor (SByte*, Int32, Int32)

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.  

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

Visual Basic (Declaration)
Visual Basic does not support APIs that consume or return unsafe types.
Visual Basic (Usage)
Visual Basic does not support APIs that consume or return unsafe types.
C#
[CLSCompliantAttribute(false)] 
public String (
    sbyte* value,
    int startIndex,
    int length
)
C++
[CLSCompliantAttribute(false)] 
public:
String (
    signed char* value, 
    int startIndex, 
    int length
)
J#
J# does not support APIs that consume or return unsafe types.
JScript
JScript does not support APIs that consume or return unsafe types.

Parameters

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.

Exceptions

Exception typeCondition

ArgumentNullException

value is a null reference (Nothing in Visual Basic).

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 in ASCII.

AccessViolationException

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

Remarks

The value parameter is assumed to point to an array representing a string encoded in ASCII; that is, the string is encoded using the ANSI code page.

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.

Example

The following simple code example demonstrates how you can create an instance of the String class with this constructor.

C#
unsafe
{
    // Null terminated ASCII characters in an sbyte array
    String szAsciiUpper = null;
    sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 };
    // Instruct the Garbage Collector not to move the memory
    fixed(sbyte* pAsciiUpper = sbArr1)
    {
        szAsciiUpper = new String(pAsciiUpper);
    }
    String szAsciiLower = null;
    sbyte[] sbArr2 = { 0x61, 0x62, 0x63, 0x00 };
    // Instruct the Garbage Collector not to move the memory
    fixed(sbyte* pAsciiLower = sbArr2)
    {
        szAsciiLower = new String(pAsciiLower, 0, sbArr2.Length);
    }
    // Prints "ABC abc"
    Console.WriteLine(szAsciiUpper + " " + szAsciiLower);

    // Compare Strings - the result is true
    Console.WriteLine("The Strings are equal when capitalized ? " +
        (String.Compare(szAsciiUpper.ToUpper(), szAsciiLower.ToUpper())==0?"true":"false") );

    // This is the effective equivalent of another Compare method, which ignores case
    Console.WriteLine("The Strings are equal when capitalized ? " +
        (String.Compare(szAsciiUpper, szAsciiLower, true)==0?"true":"false") );
}
C++
// Null terminated ASCII characters in a simple char array
char charArray3[4] = {0x41,0x42,0x43,0x00};
char * pstr3 =  &charArray3[ 0 ];
String^ szAsciiUpper = gcnew String( pstr3 );
char charArray4[4] = {0x61,0x62,0x63,0x00};
char * pstr4 =  &charArray4[ 0 ];
String^ szAsciiLower = gcnew String( pstr4,0,sizeof(charArray4) );

// Prints "ABC abc"
Console::WriteLine( String::Concat( szAsciiUpper,  " ", szAsciiLower ) );

// Compare Strings - the result is true
Console::WriteLine( String::Concat(  "The Strings are equal when capitalized ? ", (0 == String::Compare( szAsciiUpper->ToUpper(), szAsciiLower->ToUpper() ) ? (String^)"TRUE" :  "FALSE") ) );

// This is the effective equivalent of another Compare method, which ignores case
Console::WriteLine( String::Concat(  "The Strings are equal when capitalized ? ", (0 == String::Compare( szAsciiUpper, szAsciiLower, true ) ? (String^)"TRUE" :  "FALSE") ) );
Platforms

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0
See Also

Tags :


Community Content

Shawn Steele [MSFT]
Use Unicode :0). This doc probably intends ANSI instead of ASCII.

The doc writer undoubtedly meant ANSI code page, not ASCII where it says ASCII.  The input byte array is decoded using Encoding.Default, the system default ANSI code page, which could be a single byte code page like windows-1252 or a double byte code page like EUC-JP

I'd recommend using Unicode instead when possible, particularly since the ansi code page may be different on different machines or even at different times on the same machine.

 

Tags :

Page view tracker