String Manipulation · Manipulação de Strings
| English | Português |
|---|---|
| immutable/ɪˈmjuːtəbl/ | imutáveis |
Strings and indexing
- A
Stringis a sequence of characters, numbered from0. - In
"HELLO", index0is'H', index1is'E', … index4is'O'. s.length()gives the count of characters; the last index islength() - 1.- Off-by-one errors here are the most common String bug.
Strings e indexação
- Uma
Stringé uma sequência de caracteres, numerados a partir de0. - Em
"HELLO", índice0é'H', índice1é'E', … índice4é'O'. s.length()dá a contagem de caracteres; o último índice élength() - 1.- Erros off-by-one aqui são o bug mais comum de String.
substring
s.substring(start, end)returns the characters from indexstartup to but not includingend."HELLO".substring(1, 3)→"EL"(indices1and2, not3).s.substring(start)(one argument) goes fromstartto the end.- The result's length is
end - start.
substring
s.substring(start, end)retorna os caracteres do índicestartaté, mas não incluindoend."HELLO".substring(1, 3)→"EL"(índices1e2, não3).s.substring(start)(um argumento) vai destartaté o final.- O comprimento do resultado é
end - start.
indexOf and equals
s.indexOf("lo")returns the first index where the text appears, or-1if not found.s.equals(t)tests whether two Strings have the same characters — returns aboolean.- Use
.equals(), never==, to compare String contents. ==compares references (addresses), not the characters.
indexOf e equals
s.indexOf("lo")retorna o primeiro índice onde o texto aparece, ou-1se não encontrado.s.equals(t)testa se duas Strings têm os mesmos caracteres — retorna umboolean.- Use
.equals(), nunca==, para comparar conteúdos de String. ==compara referências (endereços), não os caracteres.
Strings are immutable
- A String is immutable 不可变: its characters can never be changed once created.
- Methods like
substringandtoUpperCasereturn a new String — they don't edit the original. s.toUpperCase();alone does nothing visible; you must store the result:s = s.toUpperCase();- Immutability is why String methods always hand back a fresh value.
Strings são imutáveis
- Uma String é imutável 不可变: seus caracteres nunca podem ser alterados após criados.
- Métodos como
substringetoUpperCaseretornam uma nova String — eles não editam a original. s.toUpperCase();sozinho não faz nada visível; você deve armazenar o resultado:s = s.toUpperCase();- Imutabilidade é por que métodos de String sempre devolvem um valor fresco.
Compare String contents with .equals(), never ==. == tests whether two references point to the same object, not whether the characters match — so a == b can be false even when both are "cat". And Strings are immutable: s.toUpperCase() returns a new String and leaves s unchanged unless you reassign s = s.toUpperCase();.
Compare conteúdos de String com .equals(), nunca ==. == testa se duas referências apontam para o mesmo objeto, não se os caracteres correspondem — então a == b pode ser false mesmo quando ambos são "cat". E Strings são imutáveis: s.toUpperCase() retorna uma nova String e deixa s inalterado a menos que você reatribua s = s.toUpperCase();.
Working with "HELLO":
"HELLO".length()→5; index0is'H', index4is'O'."HELLO".substring(1, 4)→"ELL"(indices1, 2, 3)."HELLO".indexOf("L")→2(firstL);.equals("hello")→false(case differs).
Trabalhando com "HELLO":
"HELLO".length()→5; índice0é'H', índice4é'O'."HELLO".substring(1, 4)→"ELL"(índices1, 2, 3)."HELLO".indexOf("L")→2(primeiroL);.equals("hello")→false(diferença de caso).
A String is indexed from 0 (last index length()-1). substring(start, end) returns indices start to end-1; indexOf finds a position (or -1). Compare contents with .equals(), not ==. Strings are immutable — methods return a new String, so reassign to keep the change.
Uma String é indexada a partir de 0 (último índice length()-1). substring(start, end) retorna índices start a end-1; indexOf encontra uma posição (ou -1). Compare conteúdos com .equals(), não ==. Strings são imutáveis — métodos retornam uma nova String, então reatribua para manter a mudança.
Indexing HELLO from 0 · Indexando HELLO começando do 0
substring(1, 4) takes indices 1, 2, 3 → ELL. · substring(1, 4) pega os índices 1, 2, 3 → ELL.
In the String "HELLO", which character is at index 0? · Na String "HELLO", qual caractere está no índice 0?
Strings are indexed from 0, so index 0 is 'H'. · Strings são indexadas a partir de 0, então o índice 0 é 'H'.
What does "HELLO".substring(1, 3) return? · O que "HELLO".substring(1, 3) retorna?
Indices 1 and 2 (up to but not including 3) → EL. · Índices 1 e 2 (até, mas sem incluir 3) → EL.
To compare whether two Strings have the same characters, you should use... · Para comparar se duas Strings têm os mesmos caracteres, você deve usar...
.equals() compares contents; == compares references. · .equals() compara conteúdos; == compara referências.
s.toUpperCase(); on its own changes the String s in place. · s.toUpperCase(); sozinho muda a String s in-place.
Strings are immutable — you must reassign s = s.toUpperCase();. · Strings são imutáveis — você deve reatribuir s = s.toUpperCase();
What does "HELLO".indexOf("L") return (the first L)? · O que "HELLO".indexOf("L") retorna (o primeiro L)?
The first 'L' is at index 2. · O primeiro 'L' está no índice 2.