Share via


Char.IsControl Method (String, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Indicates whether the character at the specified position in a specified string is categorized as a control character.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function IsControl ( _
    s As String, _
    index As Integer _
) As Boolean
public static bool IsControl(
    string s,
    int index
)

Parameters

Return Value

Type: System.Boolean
true if the character at position index in s is a control character; otherwise, false.

Exceptions

Exception Condition
ArgumentNullException

s is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

index is less than zero or greater than the last position in s.

Remarks

Character positions in a string are indexed starting from zero.

Control characters are non-printing and formatting characters, such as ACK, BEL, CR, FF, LF, and VT. The Unicode standard assigns the following code points to control characters: from \U0000 to \U001F, \U007F, and from \U0080 to \U009F. According to the Unicode standard, these values are to be interpreted as control characters unless their use is otherwise defined by an application. Valid control characters are members of the UnicodeCategory.Control category.

Examples

The following example enumerates the characters in a string and determines whether any are control characters.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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
            outputBlock.Text &= String.Format("Control character \U{0} found in position {1}.", _
             Convert.ToInt32(sentence.Chars(ctr)).ToString("X4"), ctr) & vbCrLf
         End If
      Next
   End Sub
End Module
' The example displays the following output:
'       Control character \U000D found in position 10.
'       Control character \U000A found in position 11.
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string sentence = "This is a " + "\n" + "two-line sentence.";
      for (int ctr = 0; ctr < sentence.Length; ctr++)
      {
         if (Char.IsControl(sentence, ctr))
            outputBlock.Text += String.Format("Control character \\U{0} found in position {1}.",
              Convert.ToInt32(sentence[ctr]).ToString("X4"), ctr) + "\n";

      }
   }
}
// The example displays the following output:
//       Control character \U000D found in position 10.
//       Control character \U000A found in position 11.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.