当前位置: 首页>>代码示例>>C#>>正文


C# String构造函数代码示例

本文整理汇总了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();    
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:27,代码来源:String

输出:

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();    
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:28,代码来源:String

输出:

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);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:13,代码来源:String

输出:

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") );
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:String

示例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.");
开发者ID:.NET开发者,项目名称:System,代码行数:17,代码来源: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);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:17,代码来源:String

输出:

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);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:27,代码来源:String

输出:

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") );
}
开发者ID:.NET开发者,项目名称:System,代码行数:28,代码来源:String


注:本文中的System.String.String构造函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。