עיתוק String
הוצג
15:00
שימוש באובייקטים ובשיטות
להלן שני תוכניות Java. הראשונה חסרה נקודה-פסיק. השנייה מחלקת שבע בשתי ומאחסנת את התוצאה במספר שלם. המייצר עצר את…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| immutable/ɪˈmjuːtəbl/ | בלתי משתנה |
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 means String methods do not modify the original 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();.
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.
אינדקס HELLO החל מ-0
substring(1, 4) לוקח את האינדקסים 1, 2, 3 → ELL.
במחרוזת "HELLO", איזה תווית נמצאת באינדקס 0?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
מחרוזות ממודדות החל מ-0, ולכן אינדקס 0 הוא 'H'.
מה מחזיר "HELLO".substring(1, 3)?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
אינדקסים 1 ו-2 (עד אך לא כולל 3) → EL.
כדי להשוות אם שתי מחרוזות מכילות אותם תווים, יש להשתמש ב...
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
.equals() משווה תוכן; == משווה הפניות.
s.toUpperCase(); לבדו אינו משנה את המחרוזת s במקום.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
מחרוזות הן בלתי ניתנות לשינוי — יש למנות מחדש s = s.toUpperCase();.
מה מחזיר "HELLO".indexOf("L") (ה-L הראשון)?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
ה-L הראשון נמצא באינדקס 2.
מה השתבש? רק צוות האתר רואה זאת.