Text Processing Projects - Applying String Skills in Real Scenarios

Vaibhav • September 10, 2025

In the previous articles, we explored the fundamentals of strings in C#, including how to create, compare, modify, and validate them. Now it’s time to put those skills into practice. This article focuses on text processing projects - small, real-world examples that demonstrate how to use string manipulation techniques effectively. These projects are designed to reinforce your understanding and give you confidence in applying string logic to solve practical problems.

Why Text Processing Matters

Text is everywhere - from user input and configuration files to logs, emails, and web content. Processing text correctly is essential for building robust applications. Whether you're cleaning up messy input, extracting meaningful data, or formatting output for display, string manipulation is a core skill every C# developer must master.

Text processing is not just about strings - it’s about understanding patterns, structure, and intent behind the data. The goal is to transform raw text into useful information.

Project: Cleaning Up User Input

Let’s start with a common scenario: cleaning up user input before storing or processing it. This involves trimming whitespace, normalizing casing, and validating format.

string rawName = "  Vaibhav Lawand  ";
string cleanedName = rawName.Trim().ToLowerInvariant();

Console.WriteLine(cleanedName); // "vaibhav lawand"

Here, we remove leading/trailing spaces and convert the name to lowercase using ToLowerInvariant for culture-safe normalization. This ensures consistency in storage and comparison.

Project: Extracting Domain from Email

Suppose you want to extract the domain part of an email address. This is useful for grouping users by organization or validating allowed domains.

string email = "[email protected]";
int atIndex = email.IndexOf('@');

if (atIndex != -1)
{
    string domain = email.Substring(atIndex + 1);
    Console.WriteLine(domain); // "company.com"
}

We use IndexOf to find the '@' character and Substring to extract everything after it. This pattern is simple but powerful for parsing structured strings.

Project: Detecting Forbidden Words

In chat apps or comment systems, you may want to block certain words. Here’s how to check if a message contains any forbidden terms.

string message = "This is a spam message";
string[] forbidden = { "spam", "scam", "fake" };

bool containsBadWord = forbidden.Any(word =>
    message.Contains(word, StringComparison.OrdinalIgnoreCase));

Console.WriteLine(containsBadWord); // True

We use Any from LINQ to check if any forbidden word appears in the message. The Contains method with StringComparison.OrdinalIgnoreCase ensures case-insensitive matching.

Always use explicit comparison options like OrdinalIgnoreCase to avoid unexpected behavior due to culture-sensitive defaults.

Project: Formatting a Report Header

Suppose you’re generating a report and want to format a header with a title and timestamp. String interpolation makes this easy.

string title = "Sales Report";
DateTime now = DateTime.Now;

string header = $"{title} - Generated on {now:yyyy-MM-dd HH:mm}";
Console.WriteLine(header); // "Sales Report - Generated on 2025-09-10 16:50"

This example uses $"" interpolation and custom date formatting to produce a clean, readable header. You can adjust the format string to match your locale or style preferences.

Project: Splitting CSV Data

CSV (Comma-Separated Values) is a common format for storing tabular data. Here’s how to split a line into fields.

string csvLine = "Vaibhav,Lawand,Pune,Project Engineer";
string[] fields = csvLine.Split(',');

foreach (string field in fields)
{
    Console.WriteLine(field);
}

The Split method breaks the string into an array using the comma as a delimiter. This is useful for importing data from files or APIs.

For more complex CSV parsing (e.g., quoted fields), consider using a dedicated library like CsvHelper.

Project: Joining Strings for Display

Sometimes you need to join multiple strings into a single line - for example, displaying a list of items.

string[] fruits = { "Apple", "Banana", "Cherry" };
string summary = string.Join(", ", fruits);

Console.WriteLine(summary); // "Apple, Banana, Cherry"

string.Join combines the array elements using the specified separator. This is cleaner and faster than manual concatenation.

Project: Counting Words in a Sentence

Let’s build a simple word counter. This is useful for analyzing text or enforcing limits (e.g., tweet length).

string sentence = "C# makes text processing fun and powerful.";
string[] words = sentence.Split(' ', StringSplitOptions.RemoveEmptyEntries);

Console.WriteLine($"Word count: {words.Length}"); // Word count: 6

We split the sentence by spaces and remove empty entries to get an accurate count. You can extend this to handle punctuation or multiple delimiters.

Project: Replacing Sensitive Information

Suppose you want to mask email addresses in a log file. Here’s a basic approach using Regex.

using System.Text.RegularExpressions;

string log = "User [email protected] logged in.";
string masked = Regex.Replace(log, @"[^@\s]+@[^@\s]+\.[^@\s]+", "[email hidden]");

Console.WriteLine(masked); // "User [email hidden] logged in."

This pattern matches a simple email format and replaces it with a placeholder. You can customize the regex for stricter or looser matching.

Regex is powerful but can be slow for large texts. Use compiled regex or pre-validate input when performance matters.

Project: Building a Search Feature

Let’s simulate a basic search feature that highlights matching terms in a paragraph.

string paragraph = "C# is a modern, object-oriented programming language.";
string keyword = "object";

if (paragraph.Contains(keyword, StringComparison.OrdinalIgnoreCase))
{
    Console.WriteLine($"Found: \"{keyword}\"");
}
else
{
    Console.WriteLine("Not found");
}

This example uses Contains with case-insensitive comparison to find the keyword. You can extend it to highlight or count matches.

Project: Generating a Slug from Title

Slugs are URL-friendly versions of titles. Here’s how to generate one by removing punctuation and replacing spaces.

string title = "C# Text Processing Projects!";
string slug = Regex.Replace(title.ToLowerInvariant(), @"[^a-z0-9\s]", "");
slug = slug.Replace(" ", "-");

Console.WriteLine(slug); // "c-text-processing-projects"

We remove non-alphanumeric characters and replace spaces with hyphens. This is useful for blog URLs, filenames, or identifiers.

Summary

Text processing is a practical skill that turns raw strings into structured, meaningful data. In this article, we explored real-world projects that apply string manipulation techniques - from cleaning input and extracting data to formatting output and building search features. These examples reinforce the concepts covered in Chapter 8 and prepare you for more advanced scenarios like file I/O, data persistence, and localization.

As you continue your C# journey, keep experimenting with strings. Try building your own utilities, parsers, and formatters. The more you practice, the more fluent you’ll become in handling text - one of the most common and important data types in programming.

In the next chapter, we’ll begin our deep dive into Object-Oriented Programming - starting with classes, objects, and the principles that make C# a powerful and expressive language for building real-world applications.