Strings are Things
Mostly, we've dealt with primitives so far.
Primitives are things like int
, double
, float
, bool
, char
.
String
s are classes from which we create objects. Object Oriented Programming (OOP) allows us to create abstractions to simplify the problem we're solving.
This is something we do all the time in spoken language — we refer to that collection of material and thread and (hopefully) style that you're wearing as a "shirt". We don't describe its manufacture unless we're tailors. The word "shirt" is an abstraction - a simpler way to think about something complex.
In a similar vein, a String
represents a sequence of primitive char
s. It allows us to call methods (send it messages) to manipulate that sequence of char
s and create new String
s.
Today, we'll show you some of the ways that you can do that.
Nothing to do with cats
When we use the operator +
with String
s, we call this concatenation. It's not adding them as we would add numbers, but it's appending one String
to another, resulting in a longer String
. C# knows how to turn primitives into Strings, so it does that for us behind the scenes, saving us a bit of work. What would this display?
int numberOfSuits = 4;
Console.WriteLine("I have " + numberOfSuits + " suits.");
Courtesy of Brainless Tales
Strings are Objects
When we deal with primitives, we compare them using the equals (==
) operator:
int numberOfMaids = 7;
int numberOfMops = 7;
if(numberOfMaids == numberOfMops) {
Console.WriteLine("They might get it clear in half a year.");
}
When we compare objects (like String
s), we use the Equals
method:
String shoeDescription = "Their shoes were clean and neat";
if(shoeDescription.Equals("Their shoes were clean and neat")) {
Console.WriteLine("This is odd, because, you know,");
Console.WriteLine("They hadn't any feet.");
}
Inspiration courtesy of Lewis Carroll's The Walrus and The Carpenter. (The oddness about the shoes in question lies in them belonging to "Oysters".)
Just in Case
Methods we call on Strings create and return new Strings rather than changing the original String. Strings in C# are immutable. A common need is to convert a String to all uppercase or all lowercase:
String myGreeting = "Hello";
Console.WriteLine(myGreeting.ToUpper()); // print "HELLO"
Console.WriteLine(myGreeting.ToLower()); // prints "hello"
Console.WriteLine(myGreeting); // prints "Hello"
Sensitivity Training
We often use one of these methods to normalize user input. If we're asking for a color and call the ToLower()
method on the user's response before comparing it to "red", the user can type "RED", "Red", or "red" and it won't matter.
Console.WriteLine("Enter a color, please.");
String response = Console.ReadLine();
if(response.ToLower().Equals("red"))
{
Console.WriteLine("Roses can be red. Also, lilies.");
}
Length
A String's Length
property will tell us how many characters it contains.
string palindrome = "A dog, a panic, in a pagoda";
int palindromeLength = palindrome.Length;
Console.WriteLine($"{palindrome} has {palindromeLength} characters.");
Escape!
Sometimes we need special characters in Strings. The obvious example is a quotation mark. Quotation marks are what open and close Strings, so how would we include a quotation mark in a String? We do something called escaping. We refer to the backslash (\
) we use as an escape character.
Console.WriteLine("Phil said, \"You've got red on you\".");
We can also use escaping to insert special characters like newlines (\n
) and tabs (\t
):
Console.WriteLine("This is the first line.\nSecond line");
Console.WriteLine("\tThis line is indented.");
Other Useful Methods
method | description | example | result |
---|---|---|---|
Trim |
trims the whitespace from the start & end of a String ; useful to clean user input |
" bar ".Trim() |
"bar" |
ToCharArray |
returns an array of char s that make up the string. |
"bar".ToCharArray() |
['b', 'a', 'r'] |
IndexOf |
returns the index of the first occurence of a substring in this String |
"fooBar".IndexOf("Bar") |
3 |
More Useful Methods
method | description | example | result |
---|---|---|---|
Contains |
indicates whether this String contains a substring |
"fooBar".Contains("Ba") |
true |
StartsWith |
indicates whether this String starts with another |
"foo".StartsWith("f") |
true |
EndsWith |
indicates whether this String ends with another |
"baz".EndsWith("az") |
true |
Substring |
returns a new string starting at the specified index, optionally with a length | "hello".Substring(1) "hello".Substring(1, 3) |
"ello" "ell" |
Examine the String Class
Let's explore how we can use our IDE to examine the String class and discover other methods we may use. We will also find things that perplex us.
Let's do it!
- Use the
ToLower()
method with some of your previous exercises to ignore case while looking at the user's response. - Ask the user for her first name and last name. Use the
Length
property to tell her which is longer. - Ask the user for her first name and last name, then print her full name, including the necessary space between first and last name.
- Ask the user to answer your riddle. Use the appropriate method to examine the answer, ignoring case. (You supply the riddle. Finding one is half the fun.)
Let's do it moar!
-
Ask the user for two Strings. Check and see if the two Strings are equal to each other. If they are, inform the the user that the Strings are equal; otherwise, inform the user that the Strings are not equal.
- Change your code so that it ignores case in the comparison.
- Ask the user to create a password. Then have the user enter the password again to verify there were no typing errors. Display a message if the user properly or improperly matched their password entries.
- Ask the user to create a user name. Make sure the user name is at least 8 characters in length and that the user name contains the word "code" somewhere regardless of case. Greet the user by their user name if it is properly entered, otherwise ask the user to try again.