Char 結構

定義

表示 UTF-16 程式碼單位 字元。Represents a character as a UTF-16 code unit.

public value class Char : IComparable, IComparable<char>, IConvertible, IEquatable<char>
public value class Char : IComparable, IConvertible
public value class Char : IComparable, IComparable<char>, IEquatable<char>
public struct Char : IComparable, IComparable<char>, IConvertible, IEquatable<char>
[System.Serializable]
public struct Char : IComparable, IConvertible
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public struct Char : IComparable, IComparable<char>, IConvertible, IEquatable<char>
public struct Char : IComparable, IComparable<char>, IEquatable<char>
type char = struct
    interface IConvertible
type char = struct
Public Structure Char
Implements IComparable, IComparable(Of Char), IConvertible, IEquatable(Of Char)
Public Structure Char
Implements IComparable, IConvertible
Public Structure Char
Implements IComparable, IComparable(Of Char), IEquatable(Of Char)
繼承
屬性
實作

範例

下列程式碼範例示範 Char中的一些方法。The following code example demonstrates some of the methods in Char.

using namespace System;
int main()
{
   char chA = 'A';
   char ch1 = '1';
   String^ str =  "test string";
   Console::WriteLine( chA.CompareTo( 'B' ) ); // Output: "-1" (meaning 'A' is 1 less than 'B')
   Console::WriteLine( chA.Equals( 'A' ) ); // Output: "True"
   Console::WriteLine( Char::GetNumericValue( ch1 ) ); // Output: "1"
   Console::WriteLine( Char::IsControl( '\t' ) ); // Output: "True"
   Console::WriteLine( Char::IsDigit( ch1 ) ); // Output: "True"
   Console::WriteLine( Char::IsLetter( ',' ) ); // Output: "False"
   Console::WriteLine( Char::IsLower( 'u' ) ); // Output: "True"
   Console::WriteLine( Char::IsNumber( ch1 ) ); // Output: "True"
   Console::WriteLine( Char::IsPunctuation( '.' ) ); // Output: "True"
   Console::WriteLine( Char::IsSeparator( str, 4 ) ); // Output: "True"
   Console::WriteLine( Char::IsSymbol( '+' ) ); // Output: "True"
   Console::WriteLine( Char::IsWhiteSpace( str, 4 ) ); // Output: "True"
   Console::WriteLine( Char::Parse(  "S" ) ); // Output: "S"
   Console::WriteLine( Char::ToLower( 'M' ) ); // Output: "m"
   Console::WriteLine( 'x' ); // Output: "x"
}

using System;

public class CharStructureSample
{
    public static void Main()
    {
        char chA = 'A';
        char ch1 = '1';
        string str = "test string";

        Console.WriteLine(chA.CompareTo('B'));          //-----------  Output: "-1" (meaning 'A' is 1 less than 'B')
        Console.WriteLine(chA.Equals('A'));             //-----------  Output: "True"
        Console.WriteLine(Char.GetNumericValue(ch1));   //-----------  Output: "1"
        Console.WriteLine(Char.IsControl('\t'));        //-----------  Output: "True"
        Console.WriteLine(Char.IsDigit(ch1));           //-----------  Output: "True"
        Console.WriteLine(Char.IsLetter(','));          //-----------  Output: "False"
        Console.WriteLine(Char.IsLower('u'));           //-----------  Output: "True"
        Console.WriteLine(Char.IsNumber(ch1));          //-----------  Output: "True"
        Console.WriteLine(Char.IsPunctuation('.'));     //-----------  Output: "True"
        Console.WriteLine(Char.IsSeparator(str, 4));    //-----------  Output: "True"
        Console.WriteLine(Char.IsSymbol('+'));          //-----------  Output: "True"
        Console.WriteLine(Char.IsWhiteSpace(str, 4));   //-----------  Output: "True"
        Console.WriteLine(Char.Parse("S"));             //-----------  Output: "S"
        Console.WriteLine(Char.ToLower('M'));           //-----------  Output: "m"
        Console.WriteLine('x'.ToString());              //-----------  Output: "x"
    }
}
Module CharStructure

    Public Sub Main()

        Dim chA As Char
        chA = "A"c
        Dim ch1 As Char
        ch1 = "1"c
        Dim str As String
        str = "test string"

        Console.WriteLine(chA.CompareTo("B"c))          ' Output: "-1" (meaning 'A' is 1 less than 'B')
        Console.WriteLine(chA.Equals("A"c))             ' Output: "True"
        Console.WriteLine(Char.GetNumericValue(ch1))    ' Output: "1"
        Console.WriteLine(Char.IsControl(Chr(9)))       ' Output: "True"
        Console.WriteLine(Char.IsDigit(ch1))            ' Output: "True"
        Console.WriteLine(Char.IsLetter(","c))          ' Output: "False"
        Console.WriteLine(Char.IsLower("u"c))           ' Output: "True"
        Console.WriteLine(Char.IsNumber(ch1))           ' Output: "True"
        Console.WriteLine(Char.IsPunctuation("."c))     ' Output: "True"
        Console.WriteLine(Char.IsSeparator(str, 4))     ' Output: "True"
        Console.WriteLine(Char.IsSymbol("+"c))          ' Output: "True"
        Console.WriteLine(Char.IsWhiteSpace(str, 4))    ' Output: "True"
        Console.WriteLine(Char.Parse("S"))              ' Output: "S"
        Console.WriteLine(Char.ToLower("M"c))           ' Output: "m"
        Console.WriteLine("x"c.ToString())              ' Output: "x"

    End Sub

End Module

備註

.NET 會使用 Char 結構,透過 UTF-16 編碼來表示 Unicode 程式碼點。.NET uses the Char structure to represent Unicode code points by using UTF-16 encoding. Char 物件的值是其16位數值(序數)值。The value of a Char object is its 16-bit numeric (ordinal) value.

如果您不熟悉 Unicode、純量值、程式碼點、代理項配對、UTF-16 和 Rune 類型,請參閱.net 中的字元編碼簡介If you aren't familiar with Unicode, scalar values, code points, surrogate pairs, UTF-16, and the Rune type, see Introduction to character encoding in .NET.

下列各節會檢查 Char 物件與字元之間的關聯性,並討論 Char 實例所執行的一些常見工作。The following sections examine the relationship between a Char object and a character and discuss some common tasks performed with Char instances. 我們建議您考慮在 .NET Core 3.0 中引進的 Rune 類型,做為執行這些工作之 Char 的替代方法。We recommend that you consider the Rune type, introduced in .NET Core 3.0, as an alternative to Char for performing some of these tasks.

Char 物件、Unicode 字元和字串Char objects, Unicode characters, and strings

String 物件是 Char 結構的連續集合,代表文字字串。A String object is a sequential collection of Char structures that represents a string of text. 大部分的 Unicode 字元都可以由單一 Char 物件表示,但是編碼為基底字元、代理組和/或合併字元序列的字元會由多個 Char 物件表示。Most Unicode characters can be represented by a single Char object, but a character that is encoded as a base character, surrogate pair, and/or combining character sequence is represented by multiple Char objects. 基於這個理由,String 物件中的 Char 結構不一定等於單一 Unicode 字元。For this reason, a Char structure in a String object is not necessarily equivalent to a single Unicode character.

在下列情況下,會使用多個16位程式碼單位來表示單一 Unicode 字元:Multiple 16-bit code units are used to represent single Unicode characters in the following cases:

  • 字元,其中可能包含單一字元或基底字元後面接著一個或多個合併字元。Glyphs, which may consist of a single character or of a base character followed by one or more combining characters. 例如,字元ä是以 Char 物件表示,其程式碼單位為 U + 0061,後面接著其程式碼單位為 U + 0308 的 Char 物件。For example, the character ä is represented by a Char object whose code unit is U+0061 followed by a Char object whose code unit is U+0308. (您也可以透過具有程式碼單位為 U + 00E4 的單一 Char 物件來定義ä字元。)下列範例說明的字元是由兩個 Char 物件所組成。(The character ä can also be defined by a single Char object that has a code unit of U+00E4.) The following example illustrates that the character ä consists of two Char objects.

    using System;
    using System.IO;
    
    public class Example
    {
       public static void Main()
       {
          StreamWriter sw = new StreamWriter("chars1.txt");
          char[] chars = { '\u0061', '\u0308' };
          string strng = new String(chars);
          sw.WriteLine(strng);
          sw.Close();
       }
    }
    // The example produces the following output:
    //       ä
    
    Imports System.IO
    
    Module Example
       Public Sub Main()
          Dim sw As New StreamWriter("chars1.txt")
          Dim chars() As Char = { ChrW(&h0061), ChrW(&h0308) }
          Dim strng As New String(chars)
          sw.WriteLine(strng) 
          sw.Close()
       End Sub
    End Module
    ' The example produces the following output:
    '       ä
    
  • Unicode 基本多語系平面(BMP)以外的字元。Characters outside the Unicode Basic Multilingual Plane (BMP). 除了代表平面0的 BMP 以外,Unicode 還支援十六個平面。Unicode supports sixteen planes in addition to the BMP, which represents plane 0. Unicode 程式碼點會以 UTF-32 表示,其包含平面的21位值。A Unicode code point is represented in UTF-32 by a 21-bit value that includes the plane. 例如,U + 1D160 代表音樂符號第八個附注字元。For example, U+1D160 represents the MUSICAL SYMBOL EIGHTH NOTE character. 因為 UTF-16 編碼只有16個位,所以 BMP 以外的字元會以 UTF-16 中的代理項配對來表示。Because UTF-16 encoding has only 16 bits, characters outside the BMP are represented by surrogate pairs in UTF-16. 下列範例說明與 U + 1D160 (音樂符號第八個附注字元)相等的 UTF-32,是 U + D834 U + DD60。The following example illustrates that the UTF-32 equivalent of U+1D160, the MUSICAL SYMBOL EIGHTH NOTE character, is U+D834 U+DD60. U + D834 是高代理;高代理項範圍是從 U + D800 到 U + DBFF。U+D834 is the high surrogate; high surrogates range from U+D800 through U+DBFF. U + DD60 是低代理;低代理項範圍是從 U + DC00 到 U + DFFF。U+DD60 is the low surrogate; low surrogates range from U+DC00 through U+DFFF.

    using System;
    using System.IO;
    
    public class Example
    {
       public static void Main()
       {
          StreamWriter sw = new StreamWriter(@".\chars2.txt");
          int utf32 = 0x1D160;
          string surrogate = Char.ConvertFromUtf32(utf32);
          sw.WriteLine("U+{0:X6} UTF-32 = {1} ({2}) UTF-16",
                       utf32, surrogate, ShowCodePoints(surrogate));
          sw.Close();
       }
    
       private static string ShowCodePoints(string value)
       {
          string retval = null;
          foreach (var ch in value)
             retval += String.Format("U+{0:X4} ", Convert.ToUInt16(ch));
    
          return retval.Trim();
       }
    }
    // The example produces the following output:
    //       U+01D160 UTF-32 = ð (U+D834 U+DD60) UTF-16
    
    Imports System.IO
    
    Module Example
       Public Sub Main()
          Dim sw As New StreamWriter(".\chars2.txt")
          Dim utf32 As Integer = &h1D160
          Dim surrogate As String = Char.ConvertFromUtf32(utf32)
          sw.WriteLine("U+{0:X6} UTF-32 = {1} ({2}) UTF-16", 
                       utf32, surrogate, ShowCodePoints(surrogate))
          sw.Close()                    
       End Sub
    
       Private Function ShowCodePoints(value As String) As String
          Dim retval As String = Nothing
          For Each ch In value
             retval += String.Format("U+{0:X4} ", Convert.ToUInt16(ch))
          Next
          Return retval.Trim()
       End Function
    End Module
    ' The example produces the following output:
    '       U+01D160 UTF-32 = ð (U+D834 U+DD60) UTF-16
    

字元和字元類別目錄Characters and character categories

每個 Unicode 字元或有效的代理配對都屬於 Unicode 類別目錄。Each Unicode character or valid surrogate pair belongs to a Unicode category. 在 .NET Framework 中,Unicode 類別目錄是由 UnicodeCategory 列舉的成員表示,並包含 UnicodeCategory.CurrencySymbolUnicodeCategory.LowercaseLetterUnicodeCategory.SpaceSeparator之類的值,例如。In the .NET Framework, Unicode categories are represented by members of the UnicodeCategory enumeration and include values such as UnicodeCategory.CurrencySymbol, UnicodeCategory.LowercaseLetter, and UnicodeCategory.SpaceSeparator, for example.

若要判斷字元的 Unicode 分類,請呼叫 GetUnicodeCategory 方法。To determine the Unicode category of a character, call the GetUnicodeCategory method. 例如,下列範例會呼叫 GetUnicodeCategory,以顯示字串中每個字元的 Unicode 分類。For example, the following example calls the GetUnicodeCategory to display the Unicode category of each character in a string. 只有在 String 實例中沒有任何代理對時,此範例才會正常運作。The example works correctly only if there are no surrogate pairs in the String instance.

using System;
using System.Globalization;

class Example
{
   public static void Main()
   {
      // Define a string with a variety of character categories.
      String s = "The red car drove down the long, narrow, secluded road.";
      // Determine the category of each character.
      foreach (var ch in s)
         Console.WriteLine("'{0}': {1}", ch, Char.GetUnicodeCategory(ch));
   }
}
// The example displays the following output:
//      'T': UppercaseLetter
//      'h': LowercaseLetter
//      'e': LowercaseLetter
//      ' ': SpaceSeparator
//      'r': LowercaseLetter
//      'e': LowercaseLetter
//      'd': LowercaseLetter
//      ' ': SpaceSeparator
//      'c': LowercaseLetter
//      'a': LowercaseLetter
//      'r': LowercaseLetter
//      ' ': SpaceSeparator
//      'd': LowercaseLetter
//      'r': LowercaseLetter
//      'o': LowercaseLetter
//      'v': LowercaseLetter
//      'e': LowercaseLetter
//      ' ': SpaceSeparator
//      'd': LowercaseLetter
//      'o': LowercaseLetter
//      'w': LowercaseLetter
//      'n': LowercaseLetter
//      ' ': SpaceSeparator
//      't': LowercaseLetter
//      'h': LowercaseLetter
//      'e': LowercaseLetter
//      ' ': SpaceSeparator
//      'l': LowercaseLetter
//      'o': LowercaseLetter
//      'n': LowercaseLetter
//      'g': LowercaseLetter
//      ',': OtherPunctuation
//      ' ': SpaceSeparator
//      'n': LowercaseLetter
//      'a': LowercaseLetter
//      'r': LowercaseLetter
//      'r': LowercaseLetter
//      'o': LowercaseLetter
//      'w': LowercaseLetter
//      ',': OtherPunctuation
//      ' ': SpaceSeparator
//      's': LowercaseLetter
//      'e': LowercaseLetter
//      'c': LowercaseLetter
//      'l': LowercaseLetter
//      'u': LowercaseLetter
//      'd': LowercaseLetter
//      'e': LowercaseLetter
//      'd': LowercaseLetter
//      ' ': SpaceSeparator
//      'r': LowercaseLetter
//      'o': LowercaseLetter
//      'a': LowercaseLetter
//      'd': LowercaseLetter
//      '.': OtherPunctuation
Imports System.Globalization

Module Example
   Public Sub Main()
      ' Define a string with a variety of character categories.
      Dim s As String = "The car drove down the narrow, secluded road."
      ' Determine the category of each character.
      For Each ch In s
         Console.WriteLine("'{0}': {1}", ch, Char.GetUnicodeCategory(ch)) 
      Next
   End Sub
End Module
' The example displays the following output:
'       'T': UppercaseLetter
'       'h': LowercaseLetter
'       'e': LowercaseLetter
'       ' ': SpaceSeparator
'       'r': LowercaseLetter
'       'e': LowercaseLetter
'       'd': LowercaseLetter
'       ' ': SpaceSeparator
'       'c': LowercaseLetter
'       'a': LowercaseLetter
'       'r': LowercaseLetter
'       ' ': SpaceSeparator
'       'd': LowercaseLetter
'       'r': LowercaseLetter
'       'o': LowercaseLetter
'       'v': LowercaseLetter
'       'e': LowercaseLetter
'       ' ': SpaceSeparator
'       'd': LowercaseLetter
'       'o': LowercaseLetter
'       'w': LowercaseLetter
'       'n': LowercaseLetter
'       ' ': SpaceSeparator
'       't': LowercaseLetter
'       'h': LowercaseLetter
'       'e': LowercaseLetter
'       ' ': SpaceSeparator
'       'l': LowercaseLetter
'       'o': LowercaseLetter
'       'n': LowercaseLetter
'       'g': LowercaseLetter
'       ',': OtherPunctuation
'       ' ': SpaceSeparator
'       'n': LowercaseLetter
'       'a': LowercaseLetter
'       'r': LowercaseLetter
'       'r': LowercaseLetter
'       'o': LowercaseLetter
'       'w': LowercaseLetter
'       ',': OtherPunctuation
'       ' ': SpaceSeparator
'       's': LowercaseLetter
'       'e': LowercaseLetter
'       'c': LowercaseLetter
'       'l': LowercaseLetter
'       'u': LowercaseLetter
'       'd': LowercaseLetter
'       'e': LowercaseLetter
'       'd': LowercaseLetter
'       ' ': SpaceSeparator
'       'r': LowercaseLetter
'       'o': LowercaseLetter
'       'a': LowercaseLetter
'       'd': LowercaseLetter
'       '.': OtherPunctuation

就內部而言,針對 ASCII 範圍(U + 0000 到 U + 00FF)以外的字元,GetUnicodeCategory 方法取決於 CharUnicodeInfo 類別所報告的 Unicode 分類。Internally, for characters outside the ASCII range (U+0000 through U+00FF), the GetUnicodeCategory method depends on Unicode categories reported by the CharUnicodeInfo class. .NET Framework 4.6.2.NET Framework 4.6.2開始,Unicode 字元是根據Unicode 標準版本 8.0.0來分類。Starting with the .NET Framework 4.6.2.NET Framework 4.6.2, Unicode characters are classified based on The Unicode Standard, Version 8.0.0. 在從 .NET Framework 4.NET Framework 4.NET Framework 4.6.1.NET Framework 4.6.1的 .NET Framework 版本中,會根據Unicode 標準版本 6.3.0來分類。In versions of the .NET Framework from the .NET Framework 4.NET Framework 4 to the .NET Framework 4.6.1.NET Framework 4.6.1, they are classified based on The Unicode Standard, Version 6.3.0.

字元和文字元素Characters and text elements

因為單一字元可以由多個 Char 物件表示,所以與個別 Char 物件搭配使用並不一定有意義。Because a single character can be represented by multiple Char objects, it is not always meaningful to work with individual Char objects. 例如,下列範例會將代表希臘愛琴大學數位0到9的 Unicode 程式碼點,轉換成 UTF-16 編碼的程式碼單位。For instance, the following example converts the Unicode code points that represent the Aegean numbers zero through 9 to UTF-16 encoded code units. 因為它錯誤地等於具有字元的 Char 物件,所以不正確地報告產生的字串有20個字元。Because it erroneously equates Char objects with characters, it inaccurately reports that the resulting string has 20 characters.

using System;

public class Example
{
   public static void Main()
   {
      string result = String.Empty;
      for (int ctr = 0x10107; ctr <= 0x10110; ctr++)  // Range of Aegean numbers.
         result += Char.ConvertFromUtf32(ctr);

      Console.WriteLine("The string contains {0} characters.", result.Length);
   }
}
// The example displays the following output:
//     The string contains 20 characters.
Module Example
   Public Sub Main()
      Dim result As String = String.Empty
      For ctr As Integer = &h10107 To &h10110     ' Range of Aegean numbers.
         result += Char.ConvertFromUtf32(ctr)
      Next         
      Console.WriteLine("The string contains {0} characters.", result.Length) 
   End Sub
End Module
' The example displays the following output:
'     The string contains 20 characters.

您可以執行下列動作,以避免假設 Char 物件代表單一字元。You can do the following to avoid the assumption that a Char object represents a single character.

  • 您可以使用整個 String 物件,而不是使用其個別字元來表示和分析語言內容。You can work with a String object in its entirety instead of working with its individual characters to represent and analyze linguistic content.

  • 您可以使用 String.EnumerateRunes,如下列範例所示:You can use String.EnumerateRunes as shown in the following example:

    int CountLetters(string s)
    {
        int letterCount = 0;
    
        foreach (Rune rune in s.EnumerateRunes())
        {
            if (Rune.IsLetter(rune))
            { letterCount++; }
        }
    
        return letterCount;
    }
    
  • 您可以使用 StringInfo 類別來處理文字元素,而不是個別的 Char 物件。You can use the StringInfo class to work with text elements instead of individual Char objects. 下列範例會使用 StringInfo 物件來計算字串中的文字專案數,其中包含0到9的希臘愛琴大學數位。The following example uses the StringInfo object to count the number of text elements in a string that consists of the Aegean numbers zero through nine. 因為它會將單一字元視為代理程式配對,所以它會正確地報告該字串包含10個字元。Because it considers a surrogate pair a single character, it correctly reports that the string contains ten characters.

    using System;
    using System.Globalization;
    
    public class Example
    {
       public static void Main()
       {
          string result = String.Empty;
          for (int ctr = 0x10107; ctr <= 0x10110; ctr++)  // Range of Aegean numbers.
             result += Char.ConvertFromUtf32(ctr);
    
          StringInfo si = new StringInfo(result);
          Console.WriteLine("The string contains {0} characters.",
                            si.LengthInTextElements);
       }
    }
    // The example displays the following output:
    //       The string contains 10 characters.
    
    Imports System.Globalization
    
    Module Example
       Public Sub Main()
          Dim result As String = String.Empty
          For ctr As Integer = &h10107 To &h10110     ' Range of Aegean numbers.
             result += Char.ConvertFromUtf32(ctr)
          Next         
          Dim si As New StringInfo(result)
          Console.WriteLine("The string contains {0} characters.", si.LengthInTextElements) 
       End Sub
    End Module
    ' The example displays the following output:
    '       The string contains 10 characters.
    
  • 如果字串包含具有一或多個合併字元的基底字元,您可以呼叫 String.Normalize 方法,將子字串轉換成單一 UTF-16 編碼的程式碼單位。If a string contains a base character that has one or more combining characters, you can call the String.Normalize method to convert the substring to a single UTF-16 encoded code unit. 下列範例會呼叫 String.Normalize 方法,將基底字元 U + 0061 (拉丁小型字母 A)和結合字元 U + 0308 (結合分項)轉換為 U + 00E4 (拉丁文小寫字母 A 加上分個)。The following example calls the String.Normalize method to convert the base character U+0061 (LATIN SMALL LETTER A) and combining character U+0308 (COMBINING DIAERESIS) to U+00E4 (LATIN SMALL LETTER A WITH DIAERESIS).

    using System;
    
    public class Example
    {
       public static void Main()
       {
          string combining = "\u0061\u0308";
          ShowString(combining);
    
          string normalized = combining.Normalize();
          ShowString(normalized);
       }
    
       private static void ShowString(string s)
       {
          Console.Write("Length of string: {0} (", s.Length);
          for (int ctr = 0; ctr < s.Length; ctr++) {
             Console.Write("U+{0:X4}", Convert.ToUInt16(s[ctr]));
             if (ctr != s.Length - 1) Console.Write(" ");
          }
          Console.WriteLine(")\n");
       }
    }
    // The example displays the following output:
    //       Length of string: 2 (U+0061 U+0308)
    //
    //       Length of string: 1 (U+00E4)
    
    Module Example
       Public Sub Main()
          Dim combining As String = ChrW(&h0061) + ChrW(&h0308)
          ShowString(combining)
          
          Dim normalized As String = combining.Normalize()
          ShowString(normalized)
       End Sub
       
       Private Sub ShowString(s As String)
          Console.Write("Length of string: {0} (", s.Length)
          For ctr As Integer = 0 To s.Length - 1
             Console.Write("U+{0:X4}", Convert.ToUInt16(s(ctr)))
             If ctr <> s.Length - 1 Then Console.Write(" ")
          Next 
          Console.WriteLine(")")
          Console.WriteLine()
       End Sub
    End Module
    ' The example displays the following output:
    '       Length of string: 2 (U+0061 U+0308)
    '       
    '       Length of string: 1 (U+00E4)
    

一般作業Common operations

Char 結構會提供方法來比較 Char 物件、將目前 Char 物件的值轉換為另一個類型的物件,以及判斷 Char 物件的 Unicode 類別目錄:The Char structure provides methods to compare Char objects, convert the value of the current Char object to an object of another type, and determine the Unicode category of a Char object:

作法To do this 使用這些 System.Char 方法Use these System.Char methods
比較 Char 物件Compare Char objects CompareToEqualsCompareTo and Equals
將程式碼點轉換成字串Convert a code point to a string ConvertFromUtf32

另請參閱 Rune 類型。See also the Rune type.
Char 物件的 Char 物件或代理項配對轉換成程式碼點Convert a Char object or a surrogate pair of Char objects to a code point 單一字元: Convert.ToInt32(Char)For a single character: Convert.ToInt32(Char)

針對代理項組或字串中的字元: Char.ConvertToUtf32For a surrogate pair or a character in a string: Char.ConvertToUtf32

另請參閱 Rune 類型。See also the Rune type.
取得字元的 Unicode 分類Get the Unicode category of a character GetUnicodeCategory

另請參閱 Rune.GetUnicodeCategorySee also Rune.GetUnicodeCategory.
判斷字元是否在特定 Unicode 分類中,例如數位、字母、標點符號、控制字元等等。Determine whether a character is in a particular Unicode category such as digit, letter, punctuation, control character, and so on IsControlIsDigitIsHighSurrogateIsLetterIsLetterOrDigitIsLowerIsLowSurrogateIsNumberIsPunctuationIsSeparatorIsSurrogateIsSurrogatePairIsSymbolIsUpperIsWhiteSpaceIsControl, IsDigit, IsHighSurrogate, IsLetter, IsLetterOrDigit, IsLower, IsLowSurrogate, IsNumber, IsPunctuation, IsSeparator, IsSurrogate, IsSurrogatePair, IsSymbol, IsUpper, and IsWhiteSpace

另請參閱 Rune 類型上的對應方法。See also corresponding methods on the Rune type.
將代表數位的 Char 物件轉換成數數值型別Convert a Char object that represents a number to a numeric value type GetNumericValue

另請參閱 Rune.GetNumericValueSee also Rune.GetNumericValue.
將字串中的字元轉換成 Char 物件Convert a character in a string into a Char object ParseTryParseParse and TryParse
Char 物件轉換成 String 物件Convert a Char object to a String object ToString
變更 Char 物件的大小寫Change the case of a Char object ToLowerToLowerInvariantToUpperToUpperInvariantToLower, ToLowerInvariant, ToUpper, and ToUpperInvariant

另請參閱 Rune 類型上的對應方法。See also corresponding methods on the Rune type.

Char 值和 interopChar values and interop

當 managed Char 類型(以 Unicode UTF-16 編碼的程式碼單位表示)傳遞給非受控碼時,interop 封送處理器預設會將字元集轉換成 ANSI。When a managed Char type, which is represented as a Unicode UTF-16 encoded code unit, is passed to unmanaged code, the interop marshaler converts the character set to ANSI by default. 您可以將 DllImportAttribute 屬性套用至平台叫用宣告,並將 StructLayoutAttribute 屬性套用至 COM Interop 宣告,以控制已封送處理的 Char 類型所使用的字元集。You can apply the DllImportAttribute attribute to platform invoke declarations and the StructLayoutAttribute attribute to a COM interop declaration to control which character set a marshaled Char type uses.

欄位

MaxValue

表示 Char 最大的可能值。Represents the largest possible value of a Char. 這個欄位是常數。This field is constant.

MinValue

表示 Char 最小的可能值。Represents the smallest possible value of a Char. 這個欄位是常數。This field is constant.

方法

CompareTo(Char)

比較這個執行個體與指定的 Char 物件,並且表示這個執行個體在排序次序中,位於所指定 Char 物件之前、之後或相同位置。Compares this instance to a specified Char object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified Char object.

CompareTo(Object)

比較這個執行個體與指定的物件,並且表示這個執行個體在排序次序中,位於所指定 Object 之前、之後或相同位置。Compares this instance to a specified object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified Object.

ConvertFromUtf32(Int32)

將指定之 Unicode 字碼指標轉換成 UTF-16 編碼的字串。Converts the specified Unicode code point into a UTF-16 encoded string.

ConvertToUtf32(Char, Char)

將 UTF-16 編碼之 Surrogate 字組的值轉換成 Unicode 字碼指標。Converts the value of a UTF-16 encoded surrogate pair into a Unicode code point.

ConvertToUtf32(String, Int32)

將字串中指定之位置上的 UTF-16 編碼之字元或 Surrogate 字組的值轉換成 Unicode 字碼指標。Converts the value of a UTF-16 encoded character or surrogate pair at a specified position in a string into a Unicode code point.

Equals(Char)

傳回值,指出此執行個體是否等於指定的 Char 物件。Returns a value that indicates whether this instance is equal to the specified Char object.

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

GetHashCode()

傳回此執行個體的雜湊碼。Returns the hash code for this instance.

GetNumericValue(Char)

將指定的數字 Unicode 字元轉換成雙精確度浮點數。Converts the specified numeric Unicode character to a double-precision floating point number.

GetNumericValue(String, Int32)

將指定字串中指定位置的數字 Unicode 字元轉換成雙精確度浮點數。Converts the numeric Unicode character at the specified position in a specified string to a double-precision floating point number.

GetTypeCode()

傳回實值類型 TypeCodeCharReturns the TypeCode for value type Char.

GetUnicodeCategory(Char)

將指定的 Unicode 字元分類至由其中一個 UnicodeCategory 值所識別的群組。Categorizes a specified Unicode character into a group identified by one of the UnicodeCategory values.

GetUnicodeCategory(String, Int32)

將指定字串中指定位置處的字元,分類至由其中一個 UnicodeCategory 值所識別的群組。Categorizes the character at the specified position in a specified string into a group identified by one of the UnicodeCategory values.

IsControl(Char)

指示指定的 Unicode 字元是否分類為控制字元。Indicates whether the specified Unicode character is categorized as a control character.

IsControl(String, Int32)

指示指定的字串中指定位置處的字元是否分類為控制字元。Indicates whether the character at the specified position in a specified string is categorized as a control character.

IsDigit(Char)

指示指定的 Unicode 字元是否分類為十進位數字。Indicates whether the specified Unicode character is categorized as a decimal digit.

IsDigit(String, Int32)

指示指定的字串中指定位置處的字元是否分類為十進位數字。Indicates whether the character at the specified position in a specified string is categorized as a decimal digit.

IsHighSurrogate(Char)

指出指定的 Char 物件是否為高 Surrogate。Indicates whether the specified Char object is a high surrogate.

IsHighSurrogate(String, Int32)

表示字串中指定位置處的 Char 物件是否為高 Surrogate。Indicates whether the Char object at the specified position in a string is a high surrogate.

IsLetter(Char)

表示指定的 Unicode 字元是否分類為 Unicode 字母。Indicates whether the specified Unicode character is categorized as a Unicode letter.

IsLetter(String, Int32)

指示指定的字串中指定位置處的字元是否分類為 Unicode 字母。Indicates whether the character at the specified position in a specified string is categorized as a Unicode letter.

IsLetterOrDigit(Char)

指示指定的 Unicode 字元是否分類為字母或十進位數字。Indicates whether the specified Unicode character is categorized as a letter or a decimal digit.

IsLetterOrDigit(String, Int32)

指示指定的字串中指定位置處的字元是否分類為字母或十進位數字。Indicates whether the character at the specified position in a specified string is categorized as a letter or a decimal digit.

IsLower(Char)

指示指定的 Unicode 字元是否分類為小寫字母。Indicates whether the specified Unicode character is categorized as a lowercase letter.

IsLower(String, Int32)

指示指定的字串中指定位置處的字元是否分類為小寫字母。Indicates whether the character at the specified position in a specified string is categorized as a lowercase letter.

IsLowSurrogate(Char)

指出指定的 Char 物件是否為低 Surrogate。Indicates whether the specified Char object is a low surrogate.

IsLowSurrogate(String, Int32)

表示字串中指定位置處的 Char 物件是否為低 Surrogate。Indicates whether the Char object at the specified position in a string is a low surrogate.

IsNumber(Char)

指示指定的 Unicode 字元是否分類為數字。Indicates whether the specified Unicode character is categorized as a number.

IsNumber(String, Int32)

指示指定的字串中指定位置處的字元是否分類為數字。Indicates whether the character at the specified position in a specified string is categorized as a number.

IsPunctuation(Char)

指示指定的 Unicode 字元是否分類為標點符號。Indicates whether the specified Unicode character is categorized as a punctuation mark.

IsPunctuation(String, Int32)

指示指定的字串中指定位置處的字元是否分類為標點符號。Indicates whether the character at the specified position in a specified string is categorized as a punctuation mark.

IsSeparator(Char)

指示指定的 Unicode 字元是否分類為分隔符號字元。Indicates whether the specified Unicode character is categorized as a separator character.

IsSeparator(String, Int32)

指示指定的字串中指定位置處的字元是否分類為分隔符號字元。Indicates whether the character at the specified position in a specified string is categorized as a separator character.

IsSurrogate(Char)

指出指定的字元是否有 Surrogate 字碼單位。Indicates whether the specified character has a surrogate code unit.

IsSurrogate(String, Int32)

指出位於指定的字串中指定位置處的字元是否有 Surrogate 字碼單位。Indicates whether the character at the specified position in a specified string has a surrogate code unit.

IsSurrogatePair(Char, Char)

表示兩個指定的 Char 物件是否組成 Surrogate 字組。Indicates whether the two specified Char objects form a surrogate pair.

IsSurrogatePair(String, Int32)

表示字串中指定位置處的兩個相鄰 Char 物件是否組成 Surrogate 字組。Indicates whether two adjacent Char objects at a specified position in a string form a surrogate pair.

IsSymbol(Char)

指示指定的 Unicode 字元是否分類為符號字元。Indicates whether the specified Unicode character is categorized as a symbol character.

IsSymbol(String, Int32)

指示指定的字串中指定位置處的字元是否分類為符號字元。Indicates whether the character at the specified position in a specified string is categorized as a symbol character.

IsUpper(Char)

指示指定的 Unicode 字元是否分類為大寫字母。Indicates whether the specified Unicode character is categorized as an uppercase letter.

IsUpper(String, Int32)

指示指定的字串中指定位置處的字元是否分類為大寫字母。Indicates whether the character at the specified position in a specified string is categorized as an uppercase letter.

IsWhiteSpace(Char)

指示指定的 Unicode 字元是否分類為泛空白字元。Indicates whether the specified Unicode character is categorized as white space.

IsWhiteSpace(String, Int32)

指示指定的字串中指定位置處的字元是否分類為泛空白字元。Indicates whether the character at the specified position in a specified string is categorized as white space.

Parse(String)

將指定的字串值轉換成它的對等 Unicode 字元。Converts the value of the specified string to its equivalent Unicode character.

ToLower(Char)

將 Unicode 字元值轉換成它的對等小寫。Converts the value of a Unicode character to its lowercase equivalent.

ToLower(Char, CultureInfo)

使用指定的特定文化特性格式資訊,將指定的 Unicode 字元值轉換成它的對等小寫。Converts the value of a specified Unicode character to its lowercase equivalent using specified culture-specific formatting information.

ToLowerInvariant(Char)

使用不因文化特性而異 (Invariant Culture) 的轉換規則,將 Unicode 字元的值轉換成其小寫的同等項目。Converts the value of a Unicode character to its lowercase equivalent using the casing rules of the invariant culture.

ToString()

將這個執行個體的值轉換為它的相等字串表示。Converts the value of this instance to its equivalent string representation.

ToString(Char)

將指定的 Unicode 字元轉換成它的對等字串表示。Converts the specified Unicode character to its equivalent string representation.

ToString(IFormatProvider)

使用指定的特定文化特性的格式資訊,將這個執行個體的值轉換為它的對等字串表示。Converts the value of this instance to its equivalent string representation using the specified culture-specific format information.

ToUpper(Char)

將 Unicode 字元值轉換成它的對等大寫。Converts the value of a Unicode character to its uppercase equivalent.

ToUpper(Char, CultureInfo)

使用指定的特定文化特性格式資訊,將指定的 Unicode 字元值轉換成它的對等大寫。Converts the value of a specified Unicode character to its uppercase equivalent using specified culture-specific formatting information.

ToUpperInvariant(Char)

使用不因文化特性而異的轉換規則,將 Unicode 字元的值轉換成其大寫的同等項目。Converts the value of a Unicode character to its uppercase equivalent using the casing rules of the invariant culture.

TryParse(String, Char)

將指定的字串值轉換成它的對等 Unicode 字元。Converts the value of the specified string to its equivalent Unicode character. 傳回碼 (Return Code) 會指出轉換是成功或是失敗。A return code indicates whether the conversion succeeded or failed.

明確介面實作

IComparable.CompareTo(Object)

將目前的執行個體與相同類型的另一個物件相比較,並傳回整數,這個整數表示目前的執行個體在排序次序中,位於另一個物件之前、之後或相同位置。Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

IConvertible.GetTypeCode()

傳回這個執行個體的 TypeCodeReturns the TypeCode for this instance.

IConvertible.ToBoolean(IFormatProvider)

注意:不支援這項轉換。Note This conversion is not supported. 嘗試這麼做會擲回 InvalidCastExceptionAttempting to do so throws an InvalidCastException.

IConvertible.ToByte(IFormatProvider)

如需這個成員的說明,請參閱 ToByte(IFormatProvider)For a description of this member, see ToByte(IFormatProvider).

IConvertible.ToChar(IFormatProvider)

如需這個成員的說明,請參閱 ToChar(IFormatProvider)For a description of this member, see ToChar(IFormatProvider).

IConvertible.ToDateTime(IFormatProvider)

注意:不支援這項轉換。Note This conversion is not supported. 嘗試這麼做會擲回 InvalidCastExceptionAttempting to do so throws an InvalidCastException.

IConvertible.ToDecimal(IFormatProvider)

注意:不支援這項轉換。Note This conversion is not supported. 嘗試這麼做會擲回 InvalidCastExceptionAttempting to do so throws an InvalidCastException.

IConvertible.ToDouble(IFormatProvider)

注意:不支援這項轉換。Note This conversion is not supported. 嘗試這麼做會擲回 InvalidCastExceptionAttempting to do so throws an InvalidCastException.

IConvertible.ToInt16(IFormatProvider)

如需這個成員的說明,請參閱 ToInt16(IFormatProvider)For a description of this member, see ToInt16(IFormatProvider).

IConvertible.ToInt32(IFormatProvider)

如需這個成員的說明,請參閱 ToInt32(IFormatProvider)For a description of this member, see ToInt32(IFormatProvider).

IConvertible.ToInt64(IFormatProvider)

如需這個成員的說明,請參閱 ToInt64(IFormatProvider)For a description of this member, see ToInt64(IFormatProvider).

IConvertible.ToSByte(IFormatProvider)

如需這個成員的說明,請參閱 ToSByte(IFormatProvider)For a description of this member, see ToSByte(IFormatProvider).

IConvertible.ToSingle(IFormatProvider)

注意:不支援這項轉換。Note This conversion is not supported. 嘗試這麼做會擲回 InvalidCastExceptionAttempting to do so throws an InvalidCastException.

IConvertible.ToString(IFormatProvider)

使用指定限定文化特性的格式資訊,將此執行個體的值轉換為相等字串。Converts the value of this instance to an equivalent string, using the specified culture-specific formatting information.

IConvertible.ToType(Type, IFormatProvider)

如需這個成員的說明,請參閱 ToType(Type, IFormatProvider)For a description of this member, see ToType(Type, IFormatProvider).

IConvertible.ToUInt16(IFormatProvider)

如需這個成員的說明,請參閱 ToUInt16(IFormatProvider)For a description of this member, see ToUInt16(IFormatProvider).

IConvertible.ToUInt32(IFormatProvider)

如需這個成員的說明,請參閱 ToUInt32(IFormatProvider)For a description of this member, see ToUInt32(IFormatProvider).

IConvertible.ToUInt64(IFormatProvider)

如需這個成員的說明,請參閱 ToUInt64(IFormatProvider)For a description of this member, see ToUInt64(IFormatProvider).

適用於

執行緒安全性

此類型的所有成員都是安全線程。All members of this type are thread safe. 出現在修改實例狀態的成員實際上會傳回以新值初始化的新實例。Members that appear to modify instance state actually return a new instance initialized with the new value. 如同任何其他類型,讀取和寫入包含此類型之實例的共用變數必須受到鎖定保護,以確保執行緒安全性。As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety.

另請參閱