您的位置:首页 > 运维架构

Using String class constructors

2006-10-21 02:40 281 查看
There are 8 String class constructors, we can do the following:

1.

String(char* Value)
Initializes a new instance of the String class to the value indicated by a specified pointer to an array of Unicode characters. (***)
~EXAMPLE:




char[] chars = ...{'f','a','n','t','a','s','t','i','c' };


unsafe




...{


    fixed (char* vv = chars)




    ...{


        string name1 = new string(vv);


    }


}

.. name1= “fantastic
 

 
2.
String(char[] Value)
Initializes a new instance of the String class to the value indicated by an array of Unicode characters.
~EXAMPLE:






char[] chars = ...{'f','a','n','t','a','s','t','i','c' };


String name2 = new String(chars);

.. name2 = “fantastic
                                                                                                                                                                                                               

 

3.  
String(sbyte* Value)
Initializes a new instance of the String class to the value indicated by a pointer to an array of 8-bit signed integers. (***)
~EXAMPLE:


unsafe




...{


    String name3 = null;




    sbyte[] sbArr = new sbyte[] ...{ 0x41, 0x42, 0x43, 0x00 };


    // Instruct the Garbage Collector not to move the memory


    fixed (sbyte* pAsciiUpper = sbArr)




    ...{


        name3 = new String(pAsciiUpper);


    }


}

.. name3 = “ABC

                                                                                                                                                                                                               
 
 

4.

String(char c,int count)
Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.
~EXAMPLE:


string name4 = new String('Y', 20);

.. name4 = “YYYYYYYYYYYYYYYYYYYY

                                                                                                                                                                                                               
 
 

5. 

String(char* value, int StartIndex, int length)
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. (***)
~EXAMPLE:




char[] chars = ...{'f','a','n','t','a','s','t','i','c' };


unsafe




...{


    fixed (char* vv = chars)




    ...{


        string name5 = new string(vv, 2, 5);


    }


}

.. name5 = “ntast

                                                                                                                                                                                                               
 
 

6.

String(char[] Value, int StartIndex, int length)
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.
~EXAMPLE:




char[] vv = ...{'f','a','n','t','a','s','t','i','c' };


string name6 = new String(vv, 5, 3);

.. name6 = “sti”;

                                                                                                                                                                                                               
 

7. 

String(sbyte* Value, int StartIndex, int length)
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.
~EXAMPLE:


unsafe




...{


    String name7 = null;




    sbyte[] sbArr = ...{ 0x61, 0x62, 0x63, 0x00 };


    // Instruct the Garbage Collector not to move the memory


    fixed (sbyte* pAsciiLower = sbArr)




    ...{


        name7 = new String(pAsciiLower, 0, sbArr2.Length-2);


    }


}

.. name7 = “ab

                                                                                                                                                                                                               
 
 
8.

String(sbyte* value, int startIndex, int length, System.Text.Encoding enc)
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.
 


unsafe




...{


    string name8 = null;




    sbyte[] asciiChars = new sbyte[] ...{ 0x52, 0x53, 0x54, 0x54, 0x56 };


    UTF8Encoding encoding = new UTF8Encoding(true, true);


 


    // Instruct the Garbage Collector not to move the memory


    fixed (sbyte* pAsciiChars = asciiChars)




    ...{


        name8 = new String(pAsciiChars, 0, asciiChars.Length,encoding);


    }


}

 .. name8 = “RSTTV
 

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

*****
To set this compiler option in the VS development environment
1.    Open the project's Properties page.
2.    Click the Build property page.
3.    Select the Allow Unsafe Code check box.
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐