char, Unicode basics, simple parsing/formatting
Understand char (UTF-16 code unit), surrogate pairs for some characters, basic UTF-8 encoding/decoding, and simple number parsing/formatting.
char, Unicode basics, simple parsing/formatting is a free C# Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Overview
Goal: Handle low-level text safely.
- char is UTF-16
- Some symbols use surrogate pairs
- Encode/decode with UTF-8
- Parse and format numbers
char basics
char is a 16-bit UTF-16 code unit; you can inspect category helpers like char.IsLetter.
using System;
public class Program
{
public static void Main(string[] args)
{
char ch = 'A';
Console.WriteLine("ch = " + ch);
Console.WriteLine("UTF-16 code unit = " + (int)ch); // 65
Console.WriteLine("IsLetter? " + char.IsLetter(ch));
Console.WriteLine("ToLower = " + char.ToLower(ch));
}
}
Surrogate pairs
Some characters need two UTF-16 code units (a surrogate pair). Length counts code units, not user-perceived characters.
using System;
public class Program
{
public static void Main(string[] args)
{
// Smiling face U+1F603 represented as a surrogate pair in UTF-16
string s = "\uD83D\uDE03";
Console.WriteLine("Text: " + s);
Console.WriteLine("Length (chars) = " + s.Length); // 2
Console.WriteLine("First char high surrogate? " + char.IsHighSurrogate(s[0]));
Console.WriteLine("Second char low surrogate? " + char.IsLowSurrogate(s[1]));
}
}
UTF-8 roundtrip
Use System.Text.Encoding.UTF8 to convert between strings and bytes for storage or I/O.
using System;
using System.Text;
public class Program
{
public static void Main(string[] args)
{
string text = "Café ☕"; // includes an accent + emoji
byte[] bytes = Encoding.UTF8.GetBytes(text); // encode to UTF-8
string back = Encoding.UTF8.GetString(bytes); // decode
Console.WriteLine("Original: " + text);
Console.WriteLine("Bytes UTF-8 = " + bytes.Length);
Console.WriteLine("Roundtrip: " + back);
}
}
Parse & format
Parse with culture awareness; use TryParse to avoid exceptions. ToString formats values (e.g., N2, C).
using System;
using System.Globalization;
public class Program
{
public static void Main(string[] args)
{
// Parsing (safe)
int n;
bool ok = int.TryParse("123", out n);
Console.WriteLine("ok=" + ok + " n=" + n);
// Parsing (throws if invalid)
try
{
double d = double.Parse("3.14", CultureInfo.InvariantCulture);
Console.WriteLine("d=" + d);
}
catch (Exception ex)
{
Console.WriteLine("Parse error: " + ex.Message);
}
// Formatting
double price = 1234.5;
Console.WriteLine(price.ToString("N2", CultureInfo.InvariantCulture)); // 1,234.50
Console.WriteLine(price.ToString("C", CultureInfo.GetCultureInfo("en-US"))); // currency example
}
}
Trim & split
Use Trim to remove whitespace and Split to tokenize; always trim tokens for clean results.
using System;
public class Program
{
public static void Main(string[] args)
{
string input = " apple, banana , cherry ";
string trimmed = input.Trim();
string[] parts = trimmed.Split(',');
for (int i = 0; i < parts.Length; i = i + 1)
{
string item = parts[i].Trim(); // clean each token
Console.WriteLine("Item: " + item);
}
}
}
char definition
Recap
Recap: char = UTF-16 code unit; emojis may be surrogate pairs. Encode with UTF-8 for bytes. Parse safely with TryParse and format with ToString patterns.
Frequently asked questions
Is the “char, Unicode basics, simple parsing/formatting” lesson free?
Yes — the full text of “char, Unicode basics, simple parsing/formatting” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “char, Unicode basics, simple parsing/formatting”?
Understand char (UTF-16 code unit), surrogate pairs for some characters, basic UTF-8 encoding/decoding, and simple number parsing/formatting. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “char, Unicode basics, simple parsing/formatting” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Strings & Text: immutability, interpolation, verbatim, StringBuilder, comparison
- StringBuilder, comparison & culture notes
- char, Unicode basics, simple parsing/formatting