Text in C#
This blog consists of thousands of lines of code solely to show text on your screen. Displaying text is an essential part of creating software. It allows us to present data, information, or gather input in the form of text. This is how we communicate with each other.
Simply put, knowing how to handle text is a big deal.
Escape character
In C#, we use \
as the escape character, and it can be quite handy. For instance,
string mySentence = "and he said "hey dude!" ";
This won't work, we need to use an escape so the computer knows our string starts from the word and
and ends with dude
!
string mySentence = "and he said \"hey dude!\" ";\
That's the right way. By using \
, we tell our computer that this quote, "
, is for decoration and not for defining a new string.
New line
Sometimes, we need to jump to a new line. To do that, we use \n
. And yes, you guessed it right, n
stands for new line.
\
lets us skip the computer's rendering and we can combine that with other functionality.
string skipLine = "this is the first line \n this will render below";
// this is the first line
// this will render below
If you need to stick strings together, use string concatenation
:
string stringA = "hello";
string stringB = "yellow";
Console.WriteLine("You can use" + stringA + "like this" + stringB);
Wrap your strings inside " "
and use +
to add variables to the string.
There's a slicker way to do this, string interpolation
:
string aa = "123";
string bb = "456";
string cc = "789";
Console.WriteLine($" Dont forget the dollar sign, {aa} {bb} {cc}");
After opening the parenthesis, add $
and double quotes " "
, write the string you want and pop your variables in { }
. I find this way easier.
Alright, so we've covered some neat tricks to work with strings, but there's more we can do.
Built-in methods
Length
and IndexOf
are really handy methods that every programmer should use. They're used to check if anything is returned, if there's a value, or to find where that value is.
At first, you might be thinking, what's the use of these, but don't worry. You'll discover the importance of these built-in methods soon enough.
Length
Since a string is a bunch of characters, we can get the number of characters inside the string using the .Length
method.
string example = "abc";
example.Length; // returns 2. yes, 2, not 3. keep reading
Okay, maybe not that impressive, but here's a real world scenario that might clear things up.
A user needs to create a password, and it needs to be at least 4 characters.
string password = Console.ReadLine();
Console.Write("Please enter your password: ");
if (password.Length >= 4)
{
Console.WriteLine("Password meets length requirement.");
}
else
{
Console.WriteLine("Password does not meet length requirement.");
}
This code checks if the password entered by the user is at least 4 characters long.
IndexOf
Here's another useful built-in method. It lets us find where the thing we're looking for is located. It's like Sherlock Holmes, but only finds the index (position) of the item.
string word = "hello";
word.IndexOf("e");
// will return 1;
You might be confused, and ask why 1
and not 2
. Well, index starts from 0
. So, the letter h is at index 0
.
Substring
The built-in Substring
method is used to pull out parts of a string or specific characters. It can be mixed with IndexOf
for more functionality.
string petFullName = "Robert the Great IV Pup Master";
int position = petFullName.IndexOf("Great"); // 12
string petName = petFullName.Substring(position); // Great IV Pup Master
This snippet finds where Great
starts and saves it in position
. Then we use that index position value to grab the rest of the string.
We can also specify the start and end positions so it only grabs what's in between.
string random = "Hello this is a random string";
int start= 0;
int end = 4;
string grabbed = random.Substring(start, end); //"Hello"
Bracket notation
If you want to grab a character at a specific index position, you can use []
.
string random = "Hello random";
int position = random.IndexOf("H"); // returns 0
char c = random[position]; // returns "H";
Changing letter case
There are many handy built-in methods for messing with strings.
.ToUpper()
will turn the given string into upper case, and .ToLower
will turn it into lower case.
string word = "some random stuff";
word.ToUpper();
Console.WriteLine(word); // "SOME RANDOM STUFF"
word.ToLower();
Console.WriteLine(word); // "some random stuff"
These can be very useful, especially if there's a specific format we need to follow. For example, you could write a small app that takes a block of text and capitalizes the first letter and the character after a period .
. That's basically what your phone's keyboard does.