Working With Text
This section deals with strings:
Useful string methods
Converting strings to numbers
Converting numbers to strings
The
StringBuilder
class
Useful String Methods
Formatting
ToLower
converts to lowercaseToUpper
converts to uppercaseTrim
removes whitespace around the string
Searching
IndexOf(x)
returns index of first matching character or substringLastIndexOf
returns index of last matching character or substring
Manipulating
Substring(index, length)
gets a substringReplace(input, output)
replaces characters/substrings
Null checking
String.IsNullOrEmpty(str)
String.IsNullOrWhiteSpace(str)
Splitting
str.Split(splitPoint)
Conversion
Converting strings to numbers
Very often when a user inputs something, it will be in a string format. You might want to convert that string to a number, especially if the input is meant to be a number.
Pro tip: If the string is null or just whitespace, int.Parse
will throw an exception. In contrast, Convert.ToInt32
will return 0
. For this reason, Convert.ToInt32
is often safer to work with.
Converting numbers to strings
Every integer has a ToString
method that accepts an optional format string.
StringBuilder
In the System.Text
namespace, there is a StringBuilder
that allows you to create mutable strings.
In other words, it comes with a bunch of useful manipulation methods like Append
or Insert
or Remove
. You use these on your builder
instance, and it's like writing onto a piece of paper.
In contrast, while regular strings have these manipulation methods, they are more costly because they return a brand new string. So, if you're performing a lot of manipulations, it could be a good idea to use StringBuilder
.
Note: Efficient string manipulation comes at the cost of having no searching methods. There are no methods like IndexOf
or Contains
or StartsWith
.
Last updated