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.
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.
相关文章推荐
- Unable to find a value for "字段名" in object of class java.lang.String using operator "."
- Using the True License ObfuscatedString class
- Unable to find a value for "dwmc" in object of class "java.lang.String" using operator "." (null
- .Net-using-Class:String 类
- How to load a local .CSS file & JavaScript resources using iPhone UIWebView Class
- java.lang.Class.forName(String name, boolean initialize, ClassLoader loader)方法
- "undefined method `root' for nil:NilClass" error when using "pod install" 解决办法
- stringByAddingPercentEscapesUsingEncoding
- Kettle5.2 couldn't convert string [...] to a date using format [yyyy/MM/dd HH:mm:ss.SSS]
- How to: Send Data Using the WebRequest Class
- xcode 7种使用coredata遇到 Class not found, using default NSManagedObject instead.问题
- (spring+hibernate)java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava
- Delphi Question: Proper using of a Record Field/Property in a Class?
- swift3.0遇到的那些坑之NSClassFromString
- golang的panic: reflect: Call using int as type string
- mybatis "There is no getter for property named 'xx' in 'class java.lang.String"问题
- typedef与using、struct与class的区别
- mybatis中出现There is no getter for property named 'depIds' in 'class java.lang.String'的错误
- java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V错误解决方案
- mybatis There is no getter for property named 'xx' in 'class java.lang.String