Char.IsControl Char.IsControl Char.IsControl Char.IsControl Method

定義

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

多載

IsControl(Char) IsControl(Char) IsControl(Char) IsControl(Char)

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

IsControl(String, Int32) IsControl(String, Int32) IsControl(String, Int32) IsControl(String, Int32)

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

備註

控制字元的格式和其他非列印字元,例如 ACK、BEL、CR、FF、LF 和 VT。Control characters are formatting and other non-printing characters, such as ACK, BEL, CR, FF, LF, and VT. Unicode 標準會將程式碼點從 \U0000 指派給 \U001F、\U007F,以及從 \U0080 到 \U009F,以控制字元。The Unicode standard assigns code points from \U0000 to \U001F, \U007F, and from \U0080 to \U009F to control characters. 根據 Unicode 標準,除非應用程式以其他方式定義,否則這些值會被視為控制字元。According to the Unicode standard, these values are to be interpreted as control characters unless their use is otherwise defined by an application. 有效的控制字元是UnicodeCategory.Control類別目錄的成員。Valid control characters are members of the UnicodeCategory.Control category.

IsControl(Char) IsControl(Char) IsControl(Char) IsControl(Char)

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

public:
 static bool IsControl(char c);
public static bool IsControl (char c);
static member IsControl : char -> bool
Public Shared Function IsControl (c As Char) As Boolean

參數

c
Char Char Char Char

要評估的 Unicode 字元。The Unicode character to evaluate.

傳回

true 是控制字元時為 c,否則為 falsetrue if c is a control character; otherwise, false.

範例

下列範例會列出每個控制字元的 Unicode 程式碼點。The following example lists the Unicode code point of each of the control characters.

using namespace System;

void main()
{
   int charsWritten = 0;
      
   for (int ctr = 0x00; ctr <= 0xFFFF; ctr++)
   {
      wchar_t ch = ctr;
      if (Char::IsControl(ch))
      {
         Console::Write(L"\U{0:X4}    ", ctr);
         charsWritten++;
         if (charsWritten % 6 == 0)
            Console::WriteLine();
      }     
   }  
}
// The example displays the following output:
//       U0000    U0001    U0002    U0003    U0004    U0005
//       U0006    U0007    U0008    U0009    U000A    U000B
//       U000C    U000D    U000E    U000F    U0010    U0011
//       U0012    U0013    U0014    U0015    U0016    U0017
//       U0018    U0019    U001A    U001B    U001C    U001D
//       U001E    U001F    U007F    U0080    U0081    U0082
//       U0083    U0084    U0085    U0086    U0087    U0088
//       U0089    U008A    U008B    U008C    U008D    U008E
//       U008F    U0090    U0091    U0092    U0093    U0094
//       U0095    U0096    U0097    U0098    U0099    U009A
//       U009B    U009C    U009D    U009E    U009F
using System;

public class ControlChars
{
   public static void Main()
   {
      int charsWritten = 0;
      
      for (int ctr = 0x00; ctr <= 0xFFFF; ctr++)
      {
         char ch = Convert.ToChar(ctr);
         if (char.IsControl(ch))
         {
            Console.Write(@"\U{0:X4}    ", ctr);
            charsWritten++;
            if (charsWritten % 6 == 0)
               Console.WriteLine();
         }     
      }  
   }
}
// The example displays the following output to the console:
//       \U0000    \U0001    \U0002    \U0003    \U0004    \U0005
//       \U0006    \U0007    \U0008    \U0009    \U000A    \U000B
//       \U000C    \U000D    \U000E    \U000F    \U0010    \U0011
//       \U0012    \U0013    \U0014    \U0015    \U0016    \U0017
//       \U0018    \U0019    \U001A    \U001B    \U001C    \U001D
//       \U001E    \U001F    \U007F    \U0080    \U0081    \U0082
//       \U0083    \U0084    \U0085    \U0086    \U0087    \U0088
//       \U0089    \U008A    \U008B    \U008C    \U008D    \U008E
//       \U008F    \U0090    \U0091    \U0092    \U0093    \U0094
//       \U0095    \U0096    \U0097    \U0098    \U0099    \U009A
//       \U009B    \U009C    \U009D    \U009E    \U009F
Module ControlChars
   Public Sub Main()
      Dim charsWritten As Integer = 0

      For ctr As Integer = &H0 To &HFFFF
         Dim ch As Char = Convert.ToChar(ctr)
         
         If Char.IsControl(ch) Then
            Console.Write("\U{0:X4}    ", ctr)
            charsWritten += 1 
            If (charsWritten Mod 6) = 0 Then 
               Console.WriteLine()
            End If    
         End If
      Next
   End Sub
End Module
' The example displays the following output to the console:
'       \U0000    \U0001    \U0002    \U0003    \U0004    \U0005
'       \U0006    \U0007    \U0008    \U0009    \U000A    \U000B
'       \U000C    \U000D    \U000E    \U000F    \U0010    \U0011
'       \U0012    \U0013    \U0014    \U0015    \U0016    \U0017
'       \U0018    \U0019    \U001A    \U001B    \U001C    \U001D
'       \U001E    \U001F    \U007F    \U0080    \U0081    \U0082
'       \U0083    \U0084    \U0085    \U0086    \U0087    \U0088
'       \U0089    \U008A    \U008B    \U008C    \U008D    \U008E
'       \U008F    \U0090    \U0091    \U0092    \U0093    \U0094
'       \U0095    \U0096    \U0097    \U0098    \U0099    \U009A
'       \U009B    \U009C    \U009D    \U009E    \U009F

IsControl(String, Int32) IsControl(String, Int32) IsControl(String, Int32) IsControl(String, Int32)

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

public:
 static bool IsControl(System::String ^ s, int index);
public static bool IsControl (string s, int index);
static member IsControl : string * int -> bool
Public Shared Function IsControl (s As String, index As Integer) As Boolean

參數

s
String String String String

字串。A string.

index
Int32 Int32 Int32 Int32

要評估的字元在 s 中的位置。The position of the character to evaluate in s.

傳回

true 中位於 index 的字元是控制字元時為 s,否則為 falsetrue if the character at position index in s is a control character; otherwise, false.

例外狀況

index 小於零或大於 s 中的最後一個位置。index is less than zero or greater than the last position in s.

範例

下列範例會列舉字串中的字元,並判斷是否有任何控制字元。The following example enumerates the characters in a string and determines whether any are control characters.

using namespace System;

void main()
{
      String ^ sentence = "This is a " + Environment::NewLine + "two-line sentence.";
      for (int ctr = 0; ctr < sentence->Length; ctr++)
      {
         if (Char::IsControl(sentence, ctr))
           Console::WriteLine("Control character \\U{0} found in position {1}.", 
                              Convert::ToInt32(sentence[ctr]).ToString("X4"), ctr);
      }
}
// The example displays the following output:
//       Control character \U000D found in position 10.
//       Control character \U000A found in position 11.
using System;

public class ControlChar
{
   public static void Main()
   {
      string sentence = "This is a " + Environment.NewLine + "two-line sentence.";
      for (int ctr = 0; ctr < sentence.Length; ctr++)
      {
         if (Char.IsControl(sentence, ctr))
           Console.WriteLine("Control character \\U{0} found in position {1}.", 
             Convert.ToInt32(sentence[ctr]).ToString("X4"), ctr);
         
      }
   }
}
// The example displays the following output to the console:
//       Control character \U000D found in position 10.
//       Control character \U000A found in position 11.
Module ControlChar
   Public Sub Main()
      Dim sentence As String = "This is a " & vbCrLf & "two-line sentence."
      For ctr As Integer = 0 to sentence.Length - 1
         If Char.IsControl(sentence, ctr) Then
            Console.WriteLine("Control character \U{0} found in position {1}.", _
             Convert.ToInt32(sentence.Chars(ctr)).ToString("X4"), ctr)
          End If
       Next   
   End Sub
End Module
' The example displays the following output to the console:
'       Control character \U000D found in position 10.
'       Control character \U000A found in position 11.

適用於