本文整理汇总了C#中System.String.String构造函数的典型用法代码示例。如果您正苦于以下问题:C# String构造函数的具体用法?C# String怎么用?C# String使用的例子?那么恭喜您, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.String
的用法示例。
在下文中一共展示了String构造函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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("{0:X4} ", Convert.ToUInt16(ch));
Console.WriteLine();
fixed(char* chPtr = chars) {
s = new string(chPtr);
}
foreach (var ch in s)
Console.Write("{0:X4} ", Convert.ToUInt16(ch));
Console.WriteLine();
}
}
输出:
0061 0062 0063 0064 0000 0041 0042 0043 0044 0000 0061 0062 0063 0064
示例2: Main
//引入命名空间
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("{0:X4} ", Convert.ToUInt16(ch));
Console.WriteLine();
fixed(sbyte* bytePtr = bytes) {
s = new string(bytePtr);
}
foreach (var ch in s)
Console.Write("{0:X4} ", Convert.ToUInt16(ch));
Console.WriteLine();
}
}
输出:
0061 0062 0063 0064 0000 0041 0042 0043 0044 0000 0061 0062 0063 0064
示例3: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
String value1 = "This is a string.";
String value2 = value1;
Console.WriteLine(value1);
Console.WriteLine(value2);
}
}
输出:
This is a string. This is a string.
示例4: String
// 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") );
示例5: 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.");
示例6: Main
//引入命名空间
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);
}
}
输出:
Hello world!
示例7: Main
//引入命名空间
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);
}
}
输出:
Hello World
示例8: fixed
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") );
}