Lesson 5
Practical String Manipulation and Looping in C#
Lesson Overview

Welcome to another exciting C# lesson! Today, we're going to dive into the practical application of C# strings while addressing real-world problems concerning text data. Imagine you're building a web scraper that gathers news articles from various sources. Alternatively, you might be developing a text-based algorithm to analyze user reviews for a website. In both cases, you'll likely work with strings and need to analyze and manipulate them. That's why today, we'll focus on how to loop over strings and perform operations on each character within a string using C#.

Our goal for this lesson is to learn about looping concepts in C#, with a specific focus on strings. We'll dive deep into string indexing techniques and gain experience performing character operations using C#'s built-in methods. Plus, we'll explore how to handle exceptions while performing these operations.

Looping Over Strings

In C#, a string is a sequence of characters. When scraping a website, you might receive all the text as a single string. Since strings are essentially character arrays, C# allows us to loop through them using a for loop. Here's an example:

C#
1string text = "Hello, C#!"; 2for (int i = 0; i < text.Length; i++) 3{ 4 Console.WriteLine(text[i]); 5} 6// Prints: 7// H 8// e 9// l 10// l 11// o 12// , 13// ...

This for loop will print each character of the string on a new line, which proves beneficial when you need to locate specific characters or words on a web page.

String Indexing Reminder

C# strings operate under a zero-based indexing system. This setup means that we can access specific characters in the string merely by knowing their position. Let's see it in action:

C#
1try 2{ 3 string text = "Hello, C#!"; 4 char tenthChar = text[9]; 5 Console.WriteLine("The tenth character is: " + tenthChar); 6} 7catch (IndexOutOfRangeException e) 8{ 9 Console.WriteLine("Char access error message: " + e.Message); 10}

This code will output The tenth character is: !. The try block contains code that might throw an exception, while the catch block handles the exception if it occurs. Specifically, IndexOutOfRangeException is thrown when attempting to access an index outside the bounds of the string. This approach ensures your program doesn't crash and allows you to handle errors gracefully.

Character Operations

Let's now explore character operations. C# provides various built-in methods such as Char.ToUpper, Char.ToLower, and Char.IsLetter. We'll consider how such functions can prove advantageous in real-world scenarios, like data cleaning, where one might need to standardize the case of the text data.

Here are some illustrative examples:

  • The Char.ToUpper and Char.ToLower methods are useful when comparing strings irrespective of their case.
C#
1Console.WriteLine(Char.ToUpper('m')); // Prints: 'M' 2Console.WriteLine(Char.ToLower('M')); // Prints: 'm'
  • The Char.IsLetter, Char.IsDigit, and Char.IsLetterOrDigit methods are useful when you need to check whether the character satisfies a specific condition (whether it is a letter, digit, or letter/digit, respectively).
C#
1Console.WriteLine(Char.IsLetter('C')); // Prints: True 2Console.WriteLine(Char.IsLetter('+')); // Prints: False 3Console.WriteLine(Char.IsDigit('2')); // Prints: True 4Console.WriteLine(Char.IsDigit('C')); // Prints: False 5Console.WriteLine(Char.IsLetterOrDigit('C')); // Prints: True 6Console.WriteLine(Char.IsLetterOrDigit('+')); // Prints: False
Lesson Summary and Practice

Excellent work! We've learned how to work with strings by looping over them, referring to string indices, and manipulating characters using C#'s built-in methods. Furthermore, we delved into strategies to handle errors that may occur when dealing with strings in our programs.

The real world is full of tasks involving string operations. From building smart typewriters and web scrapers to AI bots, string operations will indeed prove to be a useful tool. Therefore, why wait? Get hands-on with the upcoming exercises. Your journey is just beginning! See you there!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.