- "literal" means "exactly as it's written"
- a string literal is a string whose characters are spelled out explicitly in your code
') or double quotes (
- but not both!
"My dog has fleas." 'Vermonters have a hundred words for "snow".'
- some characters can't be typed literally, so you need to use string escapes
- backslash means "the next character means something special"
- for instance backslash-n (
\n) means "newline"
- for instance backslash-n (
console.log("Roses are red,\nViolets are blue;\nCandy is sweet,\nAnd so are you.")
A string understands lots of messages. Here are a few:
"drive" + "way" 'Java' + "Script" "Bert's pal Ernie" + ' sings "Rubber Duckie"' "titanic".toUpperCase() "QUIETLY".toLowerCase() "Java".repeat(10) "banana".length "berry".charAt(1) "berry".charAt(0) "apple" "banana".includes("nan") "banana".endsWith("ana") "blueberry".replace("blue", "black")
Try all of these out in
node or the browser console!
Check out MDN String docs for more.
Lab: Combining Concepts
Let's try chaining a few of these methods together.
Given the String "this is a fantastic string" can you add methods onto it so that it gets transformed into:
THIS IS A FANTASTIC STRING
Slicing and Dicing
Every string is made of lots of other strings.
You can pull out parts of a string with the
// this means "slice from character 0 to character 4" "blueberry".slice(0, 4) // this means "slice from character 4 to the end "blueberry".slice(4)
These start and end numbers are called indexes (or indices if you're feeling fancy).
String Indexing Explained
Humans like to start counting at 1, but computers like to start counting at 0.
This can be confusing, so here's a visualization to help explain it.
Think of the indexes as pointing at the spaces between characters, as in this diagram:
| B | L | U | E | B | E | R | R | Y | 0 1 2 3 4 5 6 7 8 9
So with this picture in your mind,
- includes the character to the right of the start index
- includes the character to the left of the end index...
- ...but excludes the character to the right of the end index
Try various start and end values in the console and see what happens!
Q: A string is "a series of characters"... but what is a character?
A: a character is a number (or character code) that stands for a symbol.
(Some characters stand for unprintable symbols like
ASCII and ye shall receive-ski
- ASCII: American Standard Code for Information Interchange
- Invented in 1963
(image from Wikimedia Commons)
- ASCII only goes from 0 to 127
- Unicode is the same as ASCII for values from 0 and 127
- but Unicode goes a lot higher
- Currently more than 130,000 characters, including symbols for
- 139 modern and historic scripts
- accents and other diacritics
- various mathematical ∞, currency £, and cultural ☮ symbols
- emoji 😂
Like the following:
"😂".repeat(20) > '😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂' "😂".codePointAt(0) > 128514 String.fromCodePoint(128514) > "😂"
- Sadly, this may not work by default in Windows Cmd or PowerShell
> "apple" > "cherry" false > "banana" < "cherry" true
Strings are compared one character at a time using the Unicode values of each character.
Comparing Strings: Example
So if you say
> "apple".charCodeAt(0) 97 > "apricot".charCodeAt(0) 97 > "apple".charCodeAt(1) 112 > "apricot".charCodeAt(1) 112 > "apple".charCodeAt(2) 112 > "apricot".charCodeAt(2) 114
In the above, 112 is less than 114, so the comparison stops there and returns
String Comparison Gotcha
In ASCII and Unicode, all the uppercase letters are together (codes 65 to 90), then all lowercase letters (codes 97 to 122).
That means that all uppercase strings are less than all lowercase strings.
> "apple" < "banana" true > "apple" < "BANANA" false
The standard left-to-right code-to-code comparison algorithm is simplistic but very fast.
It works fine for many applications, but if you're dealing with user input or multiple languages...
More Practice: Strings
- Sign up on exercism.io (using your GitHub id)
- Follow the "Get started" instructions behind the "Begin walk-through" button on the right-hand side
- Do the following exercises:
- Reverse String -- this is possible to do using only built-in String methods ...but if you can't figure it out, don't worry too much; we'll come back to it when we learn about loops