C-SHARP - 4.6 String Advanced
This video deepens our knowledge of strings in C#. The keyword string is an alias of the String class of the .NET framework: every string is an object. Reminder: strings are immutable, so calling a method on one always returns a brand-new string instead of mutating the original. Below is a tour of the most useful methods grouped by purpose.
Formatting and search
ToLower()/ToUpper(): return a copy in lower or upper case.Trim(): removes spaces at the beginning and the end (not the ones inside the string).IndexOf(value)/LastIndexOf(value): return the first or last index of a substring, or-1if not found.Substring(start)/Substring(start, length): extract a portion of the string.Replace(old, new): replace a character or substring; the second overload returns the modified copy.Split(separator): cuts the string into astring[]using a separator.string.IsNullOrEmpty(s)andstring.IsNullOrWhiteSpace(s): check if the string is empty (the second also considers whitespace as empty).
string s = "I am going to the gym.";
int idx = s.IndexOf("gym");
string copie = s.Substring(idx);
string remplace = s.Replace('a', 'o');
string[] mots = s.Split(' ');
int prix = 3199;
string monetaire = prix.ToString("C"); // currency format
Substring is a overloaded method: one version takes only a starting index, another takes a starting index and a length, which is handy for notification previews that show only the first part of a long message. To convert a string to a number we can use int.Parse or the safer Convert.ToInt32 which returns 0 on an empty input instead of throwing. Conversely number.ToString() turns a number into a string, with an optional format such as "C" for currency. In the next video we apply all of these methods on Visual Studio.
Summary
This lesson covers essential advanced string methods in C# for manipulating and searching text. You'll learn how to format strings using ToLower(), ToUpper(), and Trim(), as well as how to locate characters and substrings using IndexOf(), LastIndexOf(), and Substring(). These methods are critical for real-world scenarios like cleaning user input and extracting portions of text.
Key points
- Strings are immutable in C#—once created, they cannot be modified directly; methods return new string copies
- Trim() removes leading and trailing whitespace, essential for validating user input (e.g., preventing spaces from breaking alphabetical sorting)
- IndexOf() returns the index of the first occurrence of a character or word, or -1 if not found; LastIndexOf() searches from the end but returns the index from the beginning
- Substring() extracts a portion of a string; it is overloaded with two versions: one taking a starting index (copies to end) and one taking both starting index and length
- ToLower() and ToUpper() convert all characters to lowercase and uppercase respectively, useful for case-insensitive comparisons
FAQ
What does the Trim() method do and why is it important for applications?
Trim() removes whitespace characters from the beginning and end of a string. It's important for user-facing applications because users often accidentally enter spaces when registering, which can corrupt data sorting algorithms that rely on the first character being meaningful rather than a space.
What happens when IndexOf() or LastIndexOf() doesn't find the character or word you're searching for?
Both methods return -1 to indicate that the search term was not found in the string. This allows you to handle the case programmatically when a substring doesn't exist.
What is method overloading, and how does it apply to Substring()?
Method overloading means a method can have the same name but different parameters. Substring() is overloaded: the first version takes only a starting index and extracts to the end of the string, while the second version takes a starting index and a length parameter to extract only a specific number of characters.