String Manipulation · 字符串操作
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| immutable/ɪˈmjuːtəbl/ | 不可变 | bù kě biàn |
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.
字符串与下标
String是一串字符,从0开始编号。- 在
"HELLO"中,下标0是'H',下标1是'E',……下标4是'O'。 s.length()给出字符个数;最后一个下标是length() - 1。- 这里的差一错误是最常见的字符串错误。
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)返回从下标start到但不包括end的字符。"HELLO".substring(1, 3)→"EL"(下标1和2,不含3)。s.substring(start)(一个参数)从start一直到末尾。- 结果的长度是
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 与 equals
s.indexOf("lo")返回该文本首次出现的下标,找不到则返回-1。s.equals(t)检验两个 String 是否有相同的字符——返回一个boolean。- 用
.equals(),绝不用==,来比较 String 的内容。 ==比较的是引用(地址),而非字符。
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.
字符串是不可变的
- 字符串是不可变的:一旦创建,它的字符永远不能被改变。
- 像
substring和toUpperCase这样的方法返回一个新 String——它们不编辑原来的。 - 单独的
s.toUpperCase();什么可见效果都没有;你必须存下结果:s = s.toUpperCase(); - 不可变性正是 String 方法总是交还一个新值的原因。
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();.
用 .equals() 比较 String 内容,绝不用 ==。== 检验两个引用是否指向同一个对象,而非字符是否匹配——所以即使两者都是 "cat",a == b 也可能是 false。而且 String 是不可变的:s.toUpperCase() 返回一个新 String,除非你重新赋值 s = s.toUpperCase();,否则 s 保持不变。
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).
处理 "HELLO":
"HELLO".length()→5;下标0是'H',下标4是'O'。"HELLO".substring(1, 4)→"ELL"(下标1, 2, 3)。"HELLO".indexOf("L")→2(第一个L);.equals("hello")→false(大小写不同)。
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.
String 从 0 开始下标(最后下标 length()-1)。substring(start, end) 返回下标 start 到 end-1;indexOf 找位置(或 -1)。用 .equals(),而非 == 比较内容。String 是不可变的——方法返回一个新 String,所以要重新赋值才能保留改动。
Indexing HELLO from 0 · 从 0 给 HELLO 编号
substring(1, 4) takes indices 1, 2, 3 → ELL. · substring(1, 4) 取下标 1, 2, 3 → ELL。
In the String "HELLO", which character is at index 0? · 在字符串 "HELLO" 中,下标 0 处是哪个字符?
Strings are indexed from 0, so index 0 is 'H'. · 字符串从 0 编号,所以下标 0 是 'H'。
What does "HELLO".substring(1, 3) return? · "HELLO".substring(1, 3) 返回什么?
Indices 1 and 2 (up to but not including 3) → EL. · 下标 1 和 2(到但不含 3)→ EL。
To compare whether two Strings have the same characters, you should use... · 要比较两个 String 是否有相同的字符,你应该用……
.equals() compares contents; == compares references. · .equals() 比较内容;== 比较引用。
s.toUpperCase(); on its own changes the String s in place. · 单独的 s.toUpperCase(); 会就地改变字符串 s。
Strings are immutable — you must reassign s = s.toUpperCase();. · 字符串不可变——你必须重新赋值 s = s.toUpperCase();。
What does "HELLO".indexOf("L") return (the first L)? · "HELLO".indexOf("L") 返回什么(第一个 L)?
The first 'L' is at index 2. · 第一个 'L' 在下标 2。