Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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.
