Strings
Strings are text
- A string is text inside quotes:
"Ada"or'Ada'. - Join strings with
+— this is called concatenation.
Strings são texto
- Uma string é texto entre aspas:
"Ada"ou'Ada'. - Junte strings com
+— isso é chamado de concatenação.
let first = "Ada";
console.log("Hi, " + first + "!"); // → Hi, Ada!
Mind the spaces
+joins text exactly. It never adds a space for you.- Put the space inside the quotes, where you want it to appear.
Cuidado com os espaços
+junta texto exatamente. Nunca adiciona um espaço por conta própria.- Coloque o espaço dentro das aspas, onde quer que apareça.
let pet = "Rex";
console.log("Good dog," + pet + "!"); // → Good dog,Rex! (no space)
console.log("Good dog, " + pet + "!"); // → Good dog, Rex!
Length and capitals
.lengthcounts the characters..toUpperCase()and.toLowerCase()return a copy in CAPITALS or small letters.
Comprimento e maiúsculas
.lengthconta os caracteres..toUpperCase()e.toLowerCase()retornam uma cópia em MAIÚSCULAS ou minúsculas.
console.log("hello".length); // → 5
console.log("hello".toUpperCase()); // → HELLO
Index and slice a string · Indexe e fatie uma string
Characters are numbered from 0. A slice takes from start up to — but not including — end. · Caracteres são numerados a partir de 0. Um slice vai de start até — mas não incluindo — end.
Reach inside a string
- Each character has a position, counting from 0:
text[0]is the first one. .slice(start, end)takes the part fromstartup to — but not including —end.
Acesse dentro de uma string
- Cada caractere tem uma posição, contando a partir de 0:
text[0]é o primeiro. .slice(start, end)pega a parte destartaté — mas não incluindo —end.
let word = "JAVASCRIPT";
console.log(word[0]); // → J
console.log(word.slice(0, 4)); // → JAVA
Watch out
- Positions start at
0, so the 4th character isword[3]. - String methods return a new string — the original never changes.
Atenção
- As posições começam em
0, então o 4º caractere éword[3]. - Métodos de string retornam uma nova string — a original nunca muda.
Common mistakes
- Index from
0;s.lengthis a property (no()). - Strings cannot be changed in place.
Erros comuns
- Indexe a partir de
0;s.lengthé uma propriedade (sem()). - Strings não podem ser alteradas in loco.
Now you try
- Build and inspect strings.
- Press Check answer to test it.
Agora você tenta
- Construa e inspecione strings.
- Pressione Check answer para testá-lo.
let name = "Sam". Use + to log Hello, then the name then !, so it reads Hello, Sam!. · let name = "Sam". Use + para fazer log de Hello, depois o nome então !, para que fique Hello, Sam!.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Log how many characters are in the string computer using .length (it is 8). · Faça log de quantos caracteres há na string computer usando .length (é 8).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Log the string cat in CAPITALS using .toUpperCase() (it is CAT). · Faça log da string cat em MAIÚSCULAS usando .toUpperCase() (é CAT).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
let word = "code". Log just its first · primeiro letter using word[0] (it is c). · let word = "code". Faça log apenas da sua primeira letra usando word[0] (é c).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.