C-SHARP - 4.7 Advanced String (demo)

This demo applies the advanced string methods seen in the previous video. We start with string s = "Hello World"; and chain three transformations: s.ToLower(), s.ToUpper() and s.Trim(). Trim only removes whitespace at the beginning and end. Next we showcase the search methods: s.IndexOf("gym") returns the index of the first letter, or -1 when the substring is missing. We wrap the search in an if to display a friendly message instead of a raw -1.

Then we extract a portion of the string with Substring. Calling s.Substring(s.IndexOf("after")) copies the string starting at the location of the word after. The overload with two parameters limits the length: s.Substring(0, s.IndexOf("cinema")) stops before the word cinema. We then run Replace to swap a character or a word, for example replacing every a with o, or a name with another. string.IsNullOrEmpty returns true only if the string contains nothing, while string.IsNullOrWhiteSpace also returns true for strings that contain only spaces.

string s = "I am going to the gym, then I will go to the cinema.";

int idx = s.IndexOf("gym");
string copie = s.Substring(idx);
string limite = s.Substring(0, s.IndexOf("cinema"));
string remplace = s.Replace("Jason", "Jibril");
string[] mots = s.Split(' ');
foreach (string mot in mots)
{
    Console.WriteLine(mot);
}

int prix = 3199;
var symbole = new CultureInfo("en-US");
Console.WriteLine(prix.ToString("C", symbole));
Console.WriteLine(prix.ToString("C0", symbole));

Split and currency

  • Split(' ') returns a string[]: each element is a word from the original sentence.
  • To display the array we loop with for or foreach.
  • ToString("C") formats a number as currency; the culture (e.g. en-US) chooses the symbol.
  • Add using System.Globalization; to make CultureInfo available.

Without a CultureInfo the dollar sign is replaced by a question mark because the compiler does not know which currency to use. By passing a new CultureInfo("en-US") we obtain a clean USD format; replace with fr-FR for euros. There are many more string methods documented by Microsoft — explore them when you need a specific behaviour. See you in the next lesson.

Summary

This hands-on lesson demonstrates six essential C# string manipulation methods through practical code examples. You'll learn how ToLower() and ToUpper() convert letter cases, Trim() removes unwanted whitespace, and IndexOf()/LastIndexOf() locate characters or words within strings. The instructor emphasizes writing reusable code by storing search terms in variables, then shows how Substring() extracts portions of strings based on calculated starting positions.

Key points

  • Case conversion: ToLower() and ToUpper() transform string capitalization
  • Whitespace removal: Trim() eliminates leading and trailing spaces only, preserving internal spacing
  • String searching: IndexOf() finds first occurrence; LastIndexOf() finds last occurrence; both return -1 if not found
  • Index behavior: Both search methods count positions from the string's beginning, regardless of search direction
  • Substring extraction: Use IndexOf() combined with Substring() for dynamic, reusable string copying
  • Code reusability: Store search terms in variables instead of hardcoding to enable flexible, maintainable programs

FAQ

What value does IndexOf() return when a word or character is not found?

IndexOf() returns -1 to indicate the search term does not exist in the string. Use an if-statement to check this value and provide meaningful feedback to users instead of displaying the raw -1.

How do IndexOf() and LastIndexOf() differ in their search behavior?

IndexOf() searches from the string's beginning and returns the first match location, while LastIndexOf() searches from the end and returns the last match location. Importantly, both methods return the index position counted from the start of the string.

Why should you store search terms in variables rather than hardcode them?

Variables make your code reusable and maintainable. If you need to search for different terms later or accept user input, you only change the variable value instead of rewriting the entire search logic multiple times.