Check whether the Unicode character is a separator character in C#

The Char.IsSeparator() method in C# determines whether a Unicode character belongs to the separator category. Unicode separator characters are used to separate words, lines, or paragraphs in text, such as spaces, line separators, and paragraph separators.

Syntax

Following is the syntax for the Char.IsSeparator() method −

public static bool IsSeparator(char c)
public static bool IsSeparator(string s, int index)

Parameters

  • c − The Unicode character to evaluate.

  • s − A string containing the character to evaluate.

  • index − The position of the character to evaluate in the string.

Return Value

Returns true if the character is a separator; otherwise, false.

Unicode Separator Categories Space Separator ' ' (space) '\u00A0' (NBSP) Line Separator '\u2028' Paragraph Separator '\u2029' Note: Comma ',' is NOT a separator character It belongs to the punctuation category Use Char.IsPunctuation() for punctuation marks

Using Char.IsSeparator() with Different Characters

Example 1 - Testing Comma Character

using System;

public class Demo {
    public static void Main() {
        bool res;
        char val = ',';
        Console.WriteLine("Value = " + val);
        res = Char.IsSeparator(val);
        Console.WriteLine("Is the value a separator? = " + res);
    }
}

The output of the above code is −

Value = ,
Is the value a separator? = False

Example 2 - Testing Space Character

using System;

public class Demo {
    public static void Main() {
        bool res;
        char val = ' ';
        Console.WriteLine("Value = '" + val + "'");
        res = Char.IsSeparator(val);
        Console.WriteLine("Is the value a separator? = " + res);
    }
}

The output of the above code is −

Value = ' '
Is the value a separator? = True

Using Char.IsSeparator() with String and Index

Example

using System;

public class Demo {
    public static void Main() {
        string text = "Hello World\u2028Next Line\u2029New Paragraph";
        
        Console.WriteLine("Testing characters in: " + text.Replace('\u2028', '|').Replace('\u2029', '¶'));
        
        // Test space at index 5
        Console.WriteLine("Character at index 5 (' '): " + Char.IsSeparator(text, 5));
        
        // Test line separator at index 11
        Console.WriteLine("Character at index 11 (line separator): " + Char.IsSeparator(text, 11));
        
        // Test paragraph separator at index 21
        Console.WriteLine("Character at index 21 (paragraph separator): " + Char.IsSeparator(text, 21));
        
        // Test regular letter
        Console.WriteLine("Character at index 0 ('H'): " + Char.IsSeparator(text, 0));
    }
}

The output of the above code is −

Testing characters in: Hello World|Next Line¶New Paragraph
Character at index 5 (' '): True
Character at index 11 (line separator): True
Character at index 21 (paragraph separator): True
Character at index 0 ('H'): False

Common Separator Characters

Character Unicode Code Description IsSeparator Result
' ' U+0020 Space True
'\u00A0' U+00A0 Non-breaking space True
'\u2028' U+2028 Line separator True
'\u2029' U+2029 Paragraph separator True
',' U+002C Comma (punctuation) False

Conclusion

The Char.IsSeparator() method identifies Unicode separator characters like spaces, line separators, and paragraph separators. It returns false for punctuation marks like commas, which belong to a different Unicode category. This method is useful for text processing and parsing applications.

Updated on: 2026-03-17T07:04:36+05:30

299 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements