String 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 String 类的新实例。
重载
String(Char*) |
将 String 类的新实例初始化为由指向 Unicode 字符数组的指定指针指示的值。 |
String(Char[]) |
将 String 类的新实例初始化为指定字符数组中指示的 Unicode 字符。 |
String(ReadOnlySpan<Char>) |
将 String 类的新实例初始化为指定只读范围中指示的 Unicode 字符。 |
String(SByte*) |
将 String 类的新实例初始化为由指向 8 位有符号整数数组的指针指示的值。 |
String(Char, Int32) |
将 String 类的新实例初始化为由重复指定次数的指定 Unicode 字符指示的值。 |
String(Char*, Int32, Int32) |
将 String 类的新实例初始化为由指向 Unicode 字符数组的指定指针指示的值、该数组内的起始字符位置和一个长度指示的值。 |
String(Char[], Int32, Int32) |
将 String 类的新实例初始化为由 Unicode 字符数组、该数组内的起始字符位置和一个长度指示的值。 |
String(SByte*, Int32, Int32) |
将 String 类的新实例初始化为由指向 8 位有符号整数数组的指定指针、该数组内的起始位置和一个长度指示的值。 |
String(SByte*, Int32, Int32, Encoding) |
将 String 的新实例初始化为由指向 8 位有符号整数数组的指定指针、该数组内的起始位置、长度以及 Encoding 对象指示的值。 |
注解
本节内容:
重载构造函数语法
参数
异常
我调用哪种方法?
创建字符串
处理重复字符串
实例化字符串的示例:
使用字符串分配
使用字符数组
使用字符数组的一部分并重复单个字符
使用指向字符数组的指针
使用指针和数组的范围
使用指向带符号字节数组的指针
重载构造函数语法
字符串构造函数分为两个类别:那些没有指针参数的构造函数,以及具有指针参数的构造函数。 使用指针的构造函数不符合 CLS。 此外,Visual Basic不支持使用指针,C# 要求使用指针在不安全的上下文中运行的代码。 有关详细信息,请参阅 unsafe。
有关选择重载的其他指导,请参阅我调用哪种方法?
String(Char[] value)
将新实例初始化为 Unicode 字符数组指示的值。 此构造函数 (示例) 复制 Unicode 字符。
String(Char[] value, Int32 startIndex, Int32 length)
将新实例初始化为 Unicode 字符数组指示的值、该数组中的起始字符位置以及长度 (示例) 。
String(Char c, Int32 count)
将新实例初始化为指定的 Unicode 字符所指示的值, (示例) 重复指定的次数。
String(char* value)
(不符合 CLS 的) 将新实例初始化为由指向 Unicode 字符数组的指针指示的值,该数组由空字符 (U+0000 或“\0”) 终止。 (示例) 。
权限: SecurityCriticalAttribute需要立即调用方完全信任。 此成员不能由部分受信任的或透明代码使用。
String(char* value, Int32 startIndex, Int32 length)
(不符合 CLS 的) 将新实例初始化为指向 Unicode 字符数组的指针、该数组中的起始字符位置和长度的值。 构造函数复制 Unicode 字符从value
索引startIndex
开始,以索引length``startIndex
+ 结尾 - 1 (示例) 。
权限: SecurityCriticalAttribute需要立即调用方完全信任。 此成员不能由部分受信任的或透明代码使用。
String(SByte* value)
(不符合 CLS 的) 将新实例初始化为指向 8 位带符号整数数组的指针指示的值。 假定该数组表示使用当前系统代码页 (编码的字符串,即由) 指定的 Encoding.Default 编码。 构造函数从 value
指针指定的位置开始处理字符,直到到达 null 字符 (0x00) (示例) 。
权限: SecurityCriticalAttribute需要立即调用方完全信任。 此成员不能由部分受信任的或透明代码使用。
String(SByte* value, Int32 startIndex, Int32 length)
(不符合 CLS 的) 将新实例初始化为指向 8 位带符号整数数组、该数组中的起始位置以及长度的指针指示的值。 假定该数组表示使用当前系统代码页 (编码的字符串,即由) 指定的 Encoding.Default 编码。 构造函数处理从值开始startIndex
和结尾length``startIndex
+ 的字符 - 1 (示例) 。
权限: SecurityCriticalAttribute需要立即调用方完全信任。 此成员不能由部分受信任的或透明代码使用。
String(SByte* value, Int32 startIndex, Int32 length, Encoding enc)
(不符合 CLS 的) 将新实例初始化为指向 8 位带符号整数数组的指针指示的值、该数组中的起始位置、长度和 Encoding 对象。
权限: SecurityCriticalAttribute需要立即调用方完全信任。 此成员不能由部分受信任的或透明代码使用。
参数
下面是不包含指针参数的构造函数使用 String 的参数的完整列表。 有关每个重载使用的参数,请参阅上面的重载语法。
参数 | 类型 | 说明 |
---|---|---|
value |
Char[] | Unicode 字符的数组。 |
c |
Char | 一个 Unicode 字符。 |
startIndex |
Int32 | 新字符串中第一个字符的起始位置 value 。默认值:0 |
length |
Int32 | 要包含在新字符串中的 value 字符数。默认值:30Array.Length |
count |
Int32 | 新字符串中重复字符 c 的次数。 如果 count 为零,则新对象的 String.Empty值为 。 |
下面是包含指针参数的构造函数使用 String 的参数的完整列表。 有关每个重载使用的参数,请参阅上面的重载语法。
参数 | 类型 | 描述 |
---|---|---|
value |
Char* - 或 - SByte* |
指向以 null 结尾的 Unicode 字符数组或 8 位带符号整数数组的指针。 如果 value 为 null 或为空数组,则新字符串的值为 String.Empty。 |
startIndex |
Int32 | 定义新字符串中第一个字符的数组元素的索引。 默认值:0 |
length |
Int32 | 用于创建新字符串的数组元素数。 If length is zero, the constructor creates a string whose value is String.Empty. 默认值:30Array.Length |
enc |
Encoding | 一个对象,指定数组的编码方式 value 。默认值: Encoding.Default或系统的当前 ANSI 代码页 |
例外
下面是不包含指针参数的构造函数引发的异常列表。
异常 | 条件 | 引发者 |
---|---|---|
ArgumentNullException | value 上声明的默认值为 null 。 |
String(Char[], Int32, Int32) |
ArgumentOutOfRangeException | startIndex ,length 或 count 小于零。- 或 - startIndex 与 length 的和大于 value 中的元素数。- 或 - count 小于零。 |
String(Char, Int32) String(Char[], Int32, Int32) |
下面是包含指针参数的构造函数引发的异常列表。
异常 | 条件 | 引发者 |
---|---|---|
ArgumentException | value 指定包含无效 Unicode 字符的数组。- 或 - value 或 value + startIndex 指定小于 64K 的地址。- 或 - 无法从 value 字节数组初始化新String实例,因为value 不使用默认代码页编码。 |
所有具有指针的构造函数。 |
ArgumentNullException | value 为 null。 |
String(SByte*) String(SByte*, Int32, Int32) String(SByte*, Int32, Int32, Encoding) |
ArgumentOutOfRangeException | 当前进程不具有对所有通过地址访问的字符的读取访问权限。 - 或 - startIndex 或 length 小于零,value + startIndex 导致指针溢出,或者当前进程不具有对所有寻址字符的读取访问权限。- 或 - 新字符串的长度太大,无法分配。 |
所有具有指针的构造函数。 |
AccessViolationException | value 或 value + + startIndex``length - 1,指定无效地址。 |
String(SByte*) String(SByte*, Int32, Int32) String(SByte*, Int32, Int32, Encoding) |
我调用哪种方法?
功能 | 呼叫或使用 |
---|---|
创建字符串。 | 字符串文本或现有字符串的赋值 (示例) |
从整个字符数组创建字符串。 | String(Char[]) (示例) |
从字符数组的一部分创建字符串。 | String(Char[], Int32, Int32) (示例) |
创建多次重复同一个字符的字符串。 | String(Char, Int32) (示例) |
从指向 Unicode 或宽字符数组的指针创建字符串。 | String(Char*) |
使用 Unicode 或宽字符数组的一部分创建字符串。 | String(Char*, Int32, Int32) |
从 C++ char 数组创建字符串。 |
String(SByte*), String(SByte*, Int32, Int32) - 或 - String(SByte*, Int32, Int32, Encoding) |
从 ASCII 字符创建字符串。 | ASCIIEncoding.GetString |
创建字符串
以编程方式创建字符串的最常用方法是简单的赋值, 如此示例所示。 该 String 类还包括四种类型的构造函数重载,使你可以从以下值创建字符串:
从字符数组 (UTF-16 编码字符数组) 。 可以从整个数组中的字符或部分对象创建新 String 对象。 构造 String(Char[]) 函数将数组中的所有字符复制到新字符串。 构造 String(Char[], Int32, Int32) 函数将字符从索引复制到索引
startIndex
startIndex
+length
- 1 复制到新字符串。 如果length
为零,则新字符串的值为 String.Empty。如果代码重复实例化具有相同值的字符串,则可以使用创建字符串的备用方法提高应用程序性能。 有关详细信息,请参阅 处理重复字符串。
使用 String(Char, Int32) 构造函数从重复零、一次或多次的单个字符。 如果
count
为零,则新字符串的值为 String.Empty。使用或String(Char*, Int32, Int32)构造函数,从指向以 null 结尾的字符数组的String(Char*)指针。 整个数组或指定区域都可用于初始化字符串。 构造函数从指定的指针或指定指针开始复制 Unicode 字符序列,并
startIndex
继续到数组末尾或字符length
。 Ifvalue
is a null pointer orlength
is zero, the constructor creates a string whose value is String.Empty. 如果复制操作继续到数组的末尾,并且数组未以 null 结尾,则构造函数行为依赖于系统。 此类条件可能会导致访问冲突。如果数组包含任何嵌入的 null 字符, (U+0000 或“\0”) String(Char*, Int32, Int32) ,则会调用重载,则字符串实例包含
length
包括任何嵌入 null 的字符。 以下示例显示了当指向包含两个 null 字符的 10 个元素的指针传递给 String(Char*, Int32, Int32) 该方法时会发生什么情况。 由于该地址是数组的开头,并且数组中的所有元素都将添加到字符串中,因此构造函数会实例化包含 10 个字符的字符串,包括两个嵌入的 null。 另一方面,如果相同的数组传递给 String(Char*) 构造函数,则结果是一个不包含第一个 null 字符的四个字符字符串。using namespace System; void main() { wchar_t chars[] = { L'a', L'b', L'c', L'd', L'\0', L'A', L'B', L'C', L'D', L'\0' }; Char* chPtr = chars; String^ s = gcnew String(chPtr, 0, sizeof(chars) / sizeof (wchar_t)); for each (Char ch in s) Console::Write("{0:X4} ", Convert::ToUInt16(ch)); Console::WriteLine(); s = gcnew String(chPtr); for each (Char ch in s) Console::Write("{0:X4} ", Convert::ToUInt16(ch)); Console::WriteLine(); } // The example displays the following output: // 0061 0062 0063 0064 0000 0041 0042 0043 0044 0000 // 0061 0062 0063 0064
using System; public class Example { public unsafe static void Main() { char[] chars = { 'a', 'b', 'c', 'd', '\0', 'A', 'B', 'C', 'D', '\0' }; string s = null; fixed(char* chPtr = chars) { s = new string(chPtr, 0, chars.Length); } foreach (var ch in s) Console.Write($"{(ushort)ch:X4} "); Console.WriteLine(); fixed(char* chPtr = chars) { s = new string(chPtr); } foreach (var ch in s) Console.Write($"{(ushort)ch:X4} "); Console.WriteLine(); } } // The example displays the following output: // 0061 0062 0063 0064 0000 0041 0042 0043 0044 0000 // 0061 0062 0063 0064
#nowarn "9" open System let chars = [| 'a'; 'b'; 'c'; 'd'; '\000'; 'A'; 'B'; 'C'; 'D'; '\000' |] let s = use chPtr = fixed chars String(chPtr, 0, chars.Length) for ch in s do printf $"{uint16 ch:X4} " printfn "" let s2 = use chPtr = fixed chars String chPtr for ch in s2 do printf $"{uint16 ch:X4} " printfn "" // The example displays the following output: // 0061 0062 0063 0064 0000 0041 0042 0043 0044 0000 // 0061 0062 0063 0064
数组必须包含 Unicode 字符。 在 C++ 中,这意味着字符数组必须定义为托管 Char[] 类型或非托管
wchar_t
类型。String(Char*)如果调用重载且数组未以 null 结尾,或者String(Char*, Int32, Int32)调用重载
startIndex
+length
且 -1 包含为字符序列分配内存之外的范围,则构造函数的行为依赖于系统,并且可能发生访问冲突。从指向有符号字节数组的指针。 整个数组或指定区域都可用于初始化字符串。 可以使用默认代码页编码来解释字节序列,也可以在构造函数调用中指定编码。 如果构造函数尝试从不是 null 终止的整个数组实例化字符串,或者如果数组范围从
value
+startIndex
1 到 +value
+startIndex``length
-1 超出了为数组分配的内存,则此构造函数的行为依赖于系统,并且可能会出现访问冲突。将带符号字节数组作为参数的三个构造函数主要设计为将 C++
char
数组转换为字符串,如以下示例所示:using namespace System; void main() { char chars[] = { 'a', 'b', 'c', 'd', '\x00' }; char* charPtr = chars; String^ value = gcnew String(charPtr); Console::WriteLine(value); } // The example displays the following output: // abcd
如果数组包含任何 null 字符, ('\0') 或字节,其值为 0 且 String(SByte*, Int32, Int32) 调用重载,则字符串实例包含
length
包括任何嵌入 null 的字符。 以下示例显示了当指向包含两个 null 字符的 10 个元素的指针传递给 String(SByte*, Int32, Int32) 该方法时会发生什么情况。 由于该地址是数组的开头,并且数组中的所有元素都将添加到字符串中,因此构造函数会实例化包含 10 个字符的字符串,包括两个嵌入的 null。 另一方面,如果相同的数组传递给 String(SByte*) 构造函数,则结果是一个不包含第一个 null 字符的四个字符字符串。using namespace System; void main() { char bytes[] = { 0x61, 0x62, 0x063, 0x064, 0x00, 0x41, 0x42,0x43, 0x44, 0x00 }; char* bytePtr = bytes; String^ s = gcnew String(bytePtr, 0, sizeof(bytes) / sizeof (char)); for each (Char ch in s) Console::Write("{0:X4} ", Convert::ToUInt16(ch)); Console::WriteLine(); s = gcnew String(bytePtr); for each (Char ch in s) Console::Write("{0:X4} ", Convert::ToUInt16(ch)); Console::WriteLine(); } // The example displays the following output: // 0061 0062 0063 0064 0000 0041 0042 0043 0044 0000 // 0061 0062 0063 0064
using System; public class Example { public unsafe static void Main() { sbyte[] bytes = { 0x61, 0x62, 0x063, 0x064, 0x00, 0x41, 0x42, 0x43, 0x44, 0x00 }; string s = null; fixed (sbyte* bytePtr = bytes) { s = new string(bytePtr, 0, bytes.Length); } foreach (var ch in s) Console.Write($"{(ushort)ch:X4} "); Console.WriteLine(); fixed(sbyte* bytePtr = bytes) { s = new string(bytePtr); } foreach (var ch in s) Console.Write($"{(ushort)ch:X4} "); Console.WriteLine(); } } // The example displays the following output: // 0061 0062 0063 0064 0000 0041 0042 0043 0044 0000 // 0061 0062 0063 0064
#nowarn "9" open System let bytes = [| 0x61y; 0x62y; 0x063y; 0x064y; 0x00y; 0x41y; 0x42y; 0x43y; 0x44y; 0x00y |] let s = use bytePtr = fixed bytes String(bytePtr, 0, bytes.Length) for ch in s do printf $"{uint16 ch:X4} " printfn "" let s2 = use bytePtr = fixed bytes String bytePtr for ch in s do printf $"{uint16 ch:X4} " printfn "" // The example displays the following output: // 0061 0062 0063 0064 0000 0041 0042 0043 0044 0000 // 0061 0062 0063 0064
String(SByte*)由于构造String(SByte*, Int32, Int32)函数使用默认 ANSI 代码页进行解释
value
,因此调用具有相同字节数组的构造函数可能会创建在不同系统上具有不同值的字符串。
处理重复字符串
分析或解码文本流的应用通常使用 String(Char[], Int32, Int32) 构造函数或 StringBuilder.Append(Char[], Int32, Int32) 方法将字符序列转换为字符串。 重复创建具有相同值的新字符串,而不是创建和重复使用一个字符串会浪费内存。 如果可能通过调用 String(Char[], Int32, Int32) 构造函数重复创建相同的字符串值,即使事先不知道这些相同的字符串值是什么,则可以改用查阅表。
例如,假设从包含 XML 标记和属性的文件读取和分析字符流。 分析流时,会反复遇到某些标记 (,即具有符号意义的字符序列) 。 等效于字符串“0”、“1”、“true”和“false”的令牌很可能经常出现在 XML 流中。
可以创建一个 System.Xml.NameTable 对象来保存常见字符串,而不是将每个令牌转换为新字符串。 该 NameTable 对象提高了性能,因为它检索存储的字符串,而无需分配临时内存。 遇到令牌时,请使用 NameTable.Get(Char[], Int32, Int32) 该方法从表中检索令牌。 如果令牌存在,该方法将返回相应的字符串。 如果令牌不存在,请使用 NameTable.Add(Char[], Int32, Int32) 该方法将令牌插入表中并获取相应的字符串。
示例 1:使用字符串分配
以下示例通过为其分配字符串文本来创建一个新字符串。 它通过将第一个字符串的值分配给它来创建第二个字符串。 这是实例化新 String 对象的两种最常见方法。
using namespace System;
void main()
{
String^ value1 = L"This is a string.";
String^ value2 = value1;
Console::WriteLine(value1);
Console::WriteLine(value2);
}
// The example displays the following output:
// This is a string.
// This is a string.
using System;
public class Example
{
public static void Main()
{
String value1 = "This is a string.";
String value2 = value1;
Console.WriteLine(value1);
Console.WriteLine(value2);
}
}
// The example displays the following output:
// This is a string.
// This is a string.
let value1 = "This is a string."
let value2 = value1
printfn "%s" value1
printfn "%s" value2
// The example displays the following output:
// This is a string.
// This is a string.
Module Example
Public Sub Main()
Dim value1 As String = "This is a string."
Dim value2 As String = value1
Console.WriteLine(value1)
Console.WriteLine(value2)
End Sub
End Module
' The example displays the following output:
' This is a string.
' This is a string.
示例 2:使用字符数组
以下示例演示如何从字符数组创建新 String 对象。
// Unicode Mathematical operators
wchar_t charArray1[4] = {L'\x2200',L'\x2202',L'\x200F',L'\x2205'};
wchar_t * lptstr1 = &charArray1[ 0 ];
String^ wszMathSymbols = gcnew String( lptstr1 );
// Unicode Letterlike Symbols
wchar_t charArray2[4] = {L'\x2111',L'\x2118',L'\x2122',L'\x2126'};
wchar_t * lptstr2 = &charArray2[ 0 ];
String^ wszLetterLike = gcnew String( lptstr2 );
// Compare Strings - the result is false
Console::WriteLine( String::Concat( L"The Strings are equal? ", (0 == String::Compare( wszLetterLike, wszMathSymbols ) ? (String^)"TRUE" : "FALSE") ) );
// Unicode Mathematical operators
char [] charArr1 = {'\u2200','\u2202','\u200F','\u2205'};
String szMathSymbols = new String(charArr1);
// Unicode Letterlike Symbols
char [] charArr2 = {'\u2111','\u2118','\u2122','\u2126'};
String szLetterLike = new String (charArr2);
// Compare Strings - the result is false
Console.WriteLine("The Strings are equal? " +
(String.Compare(szMathSymbols, szLetterLike)==0?"true":"false") );
// Unicode Mathematical operators
let charArr1 = [| '\u2200'; '\u2202'; '\u200F'; '\u2205' |]
let szMathSymbols = String charArr1
// Unicode Letterlike Symbols
let charArr2 = [| '\u2111'; '\u2118'; '\u2122'; '\u2126' |]
let szLetterLike = String charArr2
// Compare Strings - the result is false
printfn $"The Strings are equal? %b{String.Compare(szMathSymbols, szLetterLike) = 0}"
' Unicode Mathematical operators
Dim charArr1() As Char = {ChrW(&H2200), ChrW(&H2202), _
ChrW(&H200F), ChrW(&H2205)}
Dim szMathSymbols As New String(charArr1)
' Unicode Letterlike Symbols
Dim charArr2() As Char = {ChrW(&H2111), ChrW(&H2118), _
ChrW(&H2122), ChrW(&H2126)}
Dim szLetterLike As New String(charArr2)
' Compare Strings - the result is false
Console.WriteLine("The strings are equal? " & _
CStr(szMathSymbols.Equals(szLetterLike)))
示例 3:使用字符数组的一部分并重复单个字符
以下示例演示如何从字符数组的一部分创建新 String 对象,以及如何创建包含单个字符多个匹配项的新 String 对象。
// Create a Unicode String with 5 Greek Alpha characters
String^ szGreekAlpha = gcnew String( L'\x0391',5 );
// Create a Unicode String with a Greek Omega character
wchar_t charArray5[3] = {L'\x03A9',L'\x03A9',L'\x03A9'};
String^ szGreekOmega = gcnew String( charArray5,2,1 );
String^ szGreekLetters = String::Concat( szGreekOmega, szGreekAlpha, szGreekOmega->Clone() );
// Examine the result
Console::WriteLine( szGreekLetters );
// The first index of Alpha
int ialpha = szGreekLetters->IndexOf( L'\x0391' );
// The last index of Omega
int iomega = szGreekLetters->LastIndexOf( L'\x03A9' );
Console::WriteLine( String::Concat( "The Greek letter Alpha first appears at index ", Convert::ToString( ialpha ) ) );
Console::WriteLine( String::Concat( " and Omega last appears at index ", Convert::ToString( iomega ), " in this String." ) );
// Create a Unicode String with 5 Greek Alpha characters
String szGreekAlpha = new String('\u0391',5);
// Create a Unicode String with a Greek Omega character
String szGreekOmega = new String(new char [] {'\u03A9','\u03A9','\u03A9'},2,1);
String szGreekLetters = String.Concat(szGreekOmega, szGreekAlpha, szGreekOmega.Clone());
// Examine the result
Console.WriteLine(szGreekLetters);
// The first index of Alpha
int ialpha = szGreekLetters.IndexOf('\u0391');
// The last index of Omega
int iomega = szGreekLetters.LastIndexOf('\u03A9');
Console.WriteLine("The Greek letter Alpha first appears at index " + ialpha +
" and Omega last appears at index " + iomega + " in this String.");
// Create a Unicode String with 5 Greek Alpha characters
let szGreekAlpha = String('\u0391',5)
// Create a Unicode String with a Greek Omega character
let szGreekOmega = String([| '\u03A9'; '\u03A9'; '\u03A9' |],2,1)
let szGreekLetters = String.Concat(szGreekOmega, szGreekAlpha, szGreekOmega.Clone())
// Examine the result
printfn $"{szGreekLetters}"
// The first index of Alpha
let ialpha = szGreekLetters.IndexOf '\u0391'
// The last index of Omega
let iomega = szGreekLetters.LastIndexOf '\u03A9'
printfn $"The Greek letter Alpha first appears at index {ialpha} and Omega last appears at index {iomega} in this String."
' Create a Unicode String with 5 Greek Alpha characters
Dim szGreekAlpha As New String(ChrW(&H0391), 5)
' Create a Unicode String with a Greek Omega character
Dim szGreekOmega As New String(New Char() {ChrW(&H03A9), ChrW(&H03A9), _
ChrW(&H03A9)}, 2, 1)
Dim szGreekLetters As String = String.Concat(szGreekOmega, szGreekAlpha, _
szGreekOmega.Clone())
' Examine the result
Console.WriteLine(szGreekLetters)
' The first index of Alpha
Dim iAlpha As Integer = szGreekLetters.IndexOf(ChrW(&H0391))
' The last index of Omega
Dim iomega As Integer = szGreekLetters.LastIndexOf(ChrW(&H03A9))
Console.WriteLine("The Greek letter Alpha first appears at index {0}.", _
ialpha)
Console.WriteLIne("The Greek letter Omega last appears at index {0}.", _
iomega)
示例 4:使用指向字符数组的指针
以下示例演示如何从指向字符数组的指针创建新 String 对象。 必须使用编译器开关编译 /unsafe
C# 示例。
using namespace System;
void main()
{
wchar_t characters[] = {L'H',L'e',L'l',L'l',L'o',L' ',
L'W',L'o',L'r',L'l',L'd',L'!',L'\x0000'};
Char* charPtr = characters;
String^ value = gcnew String(charPtr);
Console::WriteLine(value);
}
// The example displays the following output:
// Hello world!
using System;
public class Example
{
public static unsafe void Main()
{
char[] characters = { 'H', 'e', 'l', 'l', 'o', ' ',
'w', 'o', 'r', 'l', 'd', '!', '\u0000' };
string value;
fixed (char* charPtr = characters) {
value = new String(charPtr);
}
Console.WriteLine(value);
}
}
// The example displays the following output:
// Hello world!
#nowarn "9"
open System
let characters =
[| 'H'; 'e'; 'l'; 'l'; 'o'; ' '
'w'; 'o'; 'r'; 'l'; 'd'; '!'; '\u0000' |]
let value =
use charPtr = fixed characters
String charPtr
printfn $"{value}"
// The example displays the following output:
// Hello world!
示例 5:从指针和数组范围实例化字符串
以下示例检查句点或感叹号字符数组的元素。 如果找到一个字符串,它将从标点符号前面的数组中的字符实例化字符串。 否则,它会实例化包含数组的全部内容的字符串。 必须使用编译器开关编译 /unsafe
C# 示例。
using namespace System;
void main()
{
wchar_t characters[] = {L'H',L'e',L'l',L'l',L'o',L' ',
L'W',L'o',L'r',L'l',L'd',L'!',L'\x0000'};
Char* charPtr = characters;
int length = 0;
Char* iterator = charPtr;
while (*iterator != '\x0000')
{
if (*iterator == L'!' || *iterator == L'.')
break;
*iterator++;
length++;
}
String^ value = gcnew String(charPtr, 0, length);
Console::WriteLine(value);
}
// The example displays the following output:
// Hello World
using System;
public class Example
{
public static unsafe void Main()
{
char[] characters = { 'H', 'e', 'l', 'l', 'o', ' ',
'w', 'o', 'r', 'l', 'd', '!', '\u0000' };
String value;
fixed (char* charPtr = characters) {
int length = 0;
Char* iterator = charPtr;
while (*iterator != '\x0000')
{
if (*iterator == '!' || *iterator == '.')
break;
iterator++;
length++;
}
value = new String(charPtr, 0, length);
}
Console.WriteLine(value);
}
}
// The example displays the following output:
// Hello World
#nowarn "9"
open System
open FSharp.NativeInterop
let characters =
[| 'H'; 'e'; 'l'; 'l'; 'o'; ' '
'w'; 'o'; 'r'; 'l'; 'd'; '!'; '\u0000' |]
[<EntryPoint>]
let main _ =
use charPtr = fixed characters
let mutable length = 0
let mutable iterator = charPtr
let mutable broken = false
while not broken && NativePtr.read iterator <> '\u0000' do
if NativePtr.read iterator = '!' || NativePtr.read iterator = '.' then
broken <- true
else
iterator <- NativePtr.add iterator 1
length <- length + 1
String(charPtr, 0, length)
|> printfn "%s"
0
// The example displays the following output:
// Hello World
示例 6:将字符串从指针实例化到有符号字节数组
以下示例演示如何使用String(SByte*)构造函数创建类的String实例。
// 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") ) );
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") );
}
// Null terminated ASCII characters in an sbyte array
let szAsciiUpper =
let sbArr1 = [| 0x41y; 0x42y; 0x43y; 0x00y |]
// Instruct the Garbage Collector not to move the memory
use pAsciiUpper = fixed sbArr1
String pAsciiUpper
let szAsciiLower =
let sbArr2 = [| 0x61y; 0x62y; 0x63y; 0x00y |]
// Instruct the Garbage Collector not to move the memory
use pAsciiLower = fixed sbArr2
String(pAsciiLower, 0, sbArr2.Length)
// Prints "ABC abc"
printfn $"{szAsciiUpper} {szAsciiLower}"
// Compare Strings - the result is true
printfn $"The Strings are equal when capitalized ? %b{String.Compare(szAsciiUpper.ToUpper(), szAsciiLower.ToUpper()) = 0}"
// This is the effective equivalent of another Compare method, which ignores case
printfn $"The Strings are equal when capitalized ? %b{String.Compare(szAsciiUpper, szAsciiLower, true) = 0}"
String(Char*)
重要
此 API 不符合 CLS。
将 String 类的新实例初始化为由指向 Unicode 字符数组的指定指针指示的值。
public:
String(char* value);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public String (char* value);
[System.CLSCompliant(false)]
public String (char* value);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
new string : nativeptr<char> -> string
[<System.CLSCompliant(false)>]
new string : nativeptr<char> -> string
参数
- value
- Char*
指向以 null 终止的 Unicode 字符数组的指针。
- 属性
例外
当前进程不具有对所有通过地址访问的字符的读取访问权限。
value
指定的数组包含无效的 Unicode 字符,或 value
指定的地址小于 64000。
注解
备注
有关此构造函数和其他 String
构造函数重载的示例和全面的用法信息,请参阅 String 构造函数摘要。
适用于
String(Char[])
将 String 类的新实例初始化为指定字符数组中指示的 Unicode 字符。
public:
String(cli::array <char> ^ value);
public String (char[] value);
public String (char[]? value);
new string : char[] -> string
Public Sub New (value As Char())
参数
- value
- Char[]
Unicode 字符的数组。
注解
备注
有关此构造函数和其他 String
构造函数重载的示例和全面的用法信息,请参阅 String 构造函数摘要。
适用于
String(ReadOnlySpan<Char>)
将 String 类的新实例初始化为指定只读范围中指示的 Unicode 字符。
public:
String(ReadOnlySpan<char> value);
public String (ReadOnlySpan<char> value);
new string : ReadOnlySpan<char> -> string
Public Sub New (value As ReadOnlySpan(Of Char))
参数
- value
- ReadOnlySpan<Char>
Unicode 字符的只读范围。
适用于
String(SByte*)
重要
此 API 不符合 CLS。
将 String 类的新实例初始化为由指向 8 位有符号整数数组的指针指示的值。
public:
String(System::SByte* value);
[System.CLSCompliant(false)]
public String (sbyte* value);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public String (sbyte* value);
[<System.CLSCompliant(false)>]
new string : nativeptr<sbyte> -> string
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
new string : nativeptr<sbyte> -> string
参数
- 属性
例外
value
上声明的默认值为 null
。
如果 value
采用 ANSI 进行编码,那么无法使用 value
初始化 String 的新实例。
要初始化的新字符串的长度(该长度由 value
的 null 终止字符确定)太大,无法分配。
value
指定的地址无效。
注解
备注
有关此构造函数和其他 String
构造函数重载的示例和全面的用法信息,请参阅 String 构造函数摘要。
适用于
String(Char, Int32)
将 String 类的新实例初始化为由重复指定次数的指定 Unicode 字符指示的值。
public:
String(char c, int count);
public String (char c, int count);
new string : char * int -> string
Public Sub New (c As Char, count As Integer)
参数
- c
- Char
一个 Unicode 字符。
- count
- Int32
c
出现的次数。
例外
count
小于零。
注解
备注
有关此构造函数和其他 String
构造函数重载的示例和全面的用法信息,请参阅 String 构造函数摘要。
适用于
String(Char*, Int32, Int32)
重要
此 API 不符合 CLS。
将 String 类的新实例初始化为由指向 Unicode 字符数组的指定指针指示的值、该数组内的起始字符位置和一个长度指示的值。
public:
String(char* value, int startIndex, int length);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public String (char* value, int startIndex, int length);
[System.CLSCompliant(false)]
public String (char* value, int startIndex, int length);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
new string : nativeptr<char> * int * int -> string
[<System.CLSCompliant(false)>]
new string : nativeptr<char> * int * int -> string
参数
- value
- Char*
指向 Unicode 字符数组的指针。
- startIndex
- Int32
value
内的起始位置。
- length
- Int32
要使用的 value
内的字符数。
- 属性
例外
startIndex
或 length
小于零,value
+ startIndex
导致指针溢出,或者当前进程不具有对所有寻址字符的读取访问权限。
value
指定的数组包含无效的 Unicode 字符,或 value
+ startIndex
指定的地址小于 64000。
注解
备注
有关此构造函数和其他 String
构造函数重载的示例和全面的用法信息,请参阅 String 构造函数摘要。
适用于
String(Char[], Int32, Int32)
将 String 类的新实例初始化为由 Unicode 字符数组、该数组内的起始字符位置和一个长度指示的值。
public:
String(cli::array <char> ^ value, int startIndex, int length);
public String (char[] value, int startIndex, int length);
new string : char[] * int * int -> string
Public Sub New (value As Char(), startIndex As Integer, length As Integer)
参数
- value
- Char[]
Unicode 字符的数组。
- startIndex
- Int32
value
内的起始位置。
- length
- Int32
要使用的 value
内的字符数。
例外
value
上声明的默认值为 null
。
注解
备注
有关此构造函数和其他 String
构造函数重载的示例和全面的用法信息,请参阅 String 构造函数摘要。
适用于
String(SByte*, Int32, Int32)
重要
此 API 不符合 CLS。
将 String 类的新实例初始化为由指向 8 位有符号整数数组的指定指针、该数组内的起始位置和一个长度指示的值。
public:
String(System::SByte* value, int startIndex, int length);
[System.CLSCompliant(false)]
public String (sbyte* value, int startIndex, int length);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public String (sbyte* value, int startIndex, int length);
[<System.CLSCompliant(false)>]
new string : nativeptr<sbyte> * int * int -> string
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
new string : nativeptr<sbyte> * int * int -> string
参数
- startIndex
- Int32
value
内的起始位置。
- length
- Int32
要使用的 value
内的字符数。
- 属性
例外
value
上声明的默认值为 null
。
startIndex
或 length
小于零。
- 或 -
由 value
+ startIndex
指定的地址对于当前平台而言太大;也就是说,此地址计算溢出。
- 或 -
要初始化的新字符串的长度太大,无法分配。
value
、startIndex
和 length
共同指定的地址无效。
注解
备注
有关此构造函数和其他 String
构造函数重载的示例和全面的用法信息,请参阅 String 构造函数摘要。
适用于
String(SByte*, Int32, Int32, Encoding)
重要
此 API 不符合 CLS。
public:
String(System::SByte* value, int startIndex, int length, System::Text::Encoding ^ enc);
[System.CLSCompliant(false)]
public String (sbyte* value, int startIndex, int length, System.Text.Encoding enc);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public String (sbyte* value, int startIndex, int length, System.Text.Encoding enc);
[<System.CLSCompliant(false)>]
new string : nativeptr<sbyte> * int * int * System.Text.Encoding -> string
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
new string : nativeptr<sbyte> * int * int * System.Text.Encoding -> string
参数
- value
- SByte*
指向 8 位带符号整数数组的指针。
- startIndex
- Int32
value
内的起始位置。
- length
- Int32
要使用的 value
内的字符数。
- enc
- Encoding
一个对象,用于指定如何对 value
所引用的数组进行编码。 如果 enc
为 null
,则假定以 ANSI 编码。
- 属性
例外
value
上声明的默认值为 null
。
startIndex
或 length
小于零。
- 或 -
由 value
+ startIndex
指定的地址对于当前平台而言太大;也就是说,此地址计算溢出。
- 或 -
要初始化的新字符串的长度太大,无法分配。
value
、startIndex
和 length
共同指定的地址无效。
注解
备注
有关此构造函数和其他 String
构造函数重载的示例和全面的用法信息,请参阅 String 构造函数摘要。