About Strings and Escape Sequences - Images: Japanese
Hey there, everyone!
Next up, let's dig into Strings and Escape Sequences. A bit further ahead in this article, we'll be covering something called Data Types — and this article lays the groundwork for that.
The word '文字列' (string of characters) in Japanese corresponds to String in English.
So, first up — Strings. Not just in Swift, but across programming in general, a sequence of characters is called a String.
Let's take another look at the sample we worked with in the previous article.
print("Hello world")
This program outputs the string "Hello world" to the debug area. Take a close look at what's inside the () — you'll notice "Hello world" is wrapped in " (double quotation marks).
That's how it works in Swift: when you want the computer to treat some text as a String, you need to wrap it in " (double quotes).
If you remove the " from that program, it'll throw an error and stop working. Make sure you don't forget them!
print(Hello world) // This causes an error.
And you're not limited to English — strings can hold just about any text.
print("こんにちは、世界。") // Japanese works just fine too.
So you can use pretty much any characters as a string — but there are a few exceptions worth knowing about.
For starters, what if you want to use " itself as part of a string? Following the pattern we've seen so far, you might try this — which ends up with three " in a row.
print(""")
This will cause an error, so watch out.
This applies to other programming languages too: you can't wrap " inside ".
The reason is that the computer can't figure out whether a given " is being used to delimit the string, or is just a plain character — so it gets confused.
"So what do I do when I actually want to use " as a plain character?" Great question. The answer is to use \ (backslash). Take a look at this sample.
print("\"")
Running this gives you:
"
It outputs " as a regular string character — exactly what we wanted.
By placing a \ right before a ", you strip it of its special meaning and treat it as just a plain " character.
Note: If you're viewing this article on Windows, the backslash may be displayed as a yen sign (¥). For more details, check out this article.
Conversely, if you want a literal \ with no special meaning, just write \\. Like this:
print("\\")
Placing a \ in front of a special character like " to strip it of its special meaning is called "escaping" it.
This comes up a lot in real-world code, so it's worth committing to memory.
So to put it plainly, \ has the ability to remove the special function from characters like ".
Saying it out loud does make it sound a bit tangled, admittedly.
You can also combine \ with certain characters to express other things — let's go through those too.
First up is combining it with the letter 'n'. Writing \n represents a newline.
Here's what that looks like:
print("abc\ndef")
Running this gives you:
abc def
You can see that the output breaks to a new line at the \n.
Also, \t represents a tab character.
print("abc\tdef")
Running this gives you:
abc def
Things like \", \\, \n, and \t that we've covered in this article are collectively called Escape Sequences (or escape characters).
It can be a little confusing at first, but the key idea is: \ on its own is just a backslash, while \ combined with another character — like \" — becomes an Escape Sequence.
These come up frequently, so make sure you remember them.
That wraps up our look at Strings! In the next article, we'll dive into Numbers.
See you there!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.