Using String class constructors

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[] 0x410x420x430x00 };
    
// 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, 25);
    }

}
.. 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 = 0x610x620x630x00 };
    
// 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[] 0x520x530x540x540x56 };
    UTF8Encoding encoding 
= new UTF8Encoding(truetrue);
 
    
// 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.
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值