Thao tác Chuỗi
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| immutable/ɪˈmjuːtəbl/ | Không đổi |
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.
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.

String indices start at 0
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.
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.
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();.
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).
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.
Truy xuất HELLO bắt đầu từ 0
substring(1, 4) lấy các chỉ mục 1, 2, 3 → ELL.
Trong chuỗi "HELLO", ký tự nào nằm ở chỉ mục 0?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Chuỗi được truy xuất từ 0, nên chỉ mục 0 là 'H'.
"HELLO".substring(1, 3) trả về gì?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Chỉ mục 1 và 2 (cho đến nhưng không bao gồm 3) → EL.
Để so sánh xem hai chuỗi có cùng nội dung hay không, bạn nên dùng...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
.equals() so sánh nội dung; == so sánh tham chiếu.
s.toUpperCase(); nếu gọi riêng lẻ sẽ thay đổi chuỗi s ngay tại chỗ.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Chuỗi bất biến — bạn phải gán lại s = s.toUpperCase();.
"HELLO".indexOf("L") trả về gì (chữ L đầu tiên)?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Chữ 'L' đầu tiên nằm ở chỉ mục 2.