Strings · สตริง (Strings)
Strings are text
- A string is text inside quotes:
"Ada"or'Ada'. - Join strings with
+— this is called concatenation.
สตริงคือข้อความ
- สตริง คือข้อความภายในเครื่องหมายอัญประกาศ:
"Ada"หรือ'Ada' - เชื่อมต่อสตริงด้วย
+- กระบวนการนี้เรียกว่า การเชื่อมสตริง
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.
ระวังช่องว่าง
+จะเชื่อมต่อข้อความ อย่างถูกต้องตามต้นฉบับ โดยไม่เพิ่มช่องว่างให้คุณอัตโนมัติ- วางช่องว่างไว้ในเครื่องหมายอัญประกาศ的位置 ที่คุณต้องการให้มันปรากฏ
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.
ความยาวและตัวพิมพ์ใหญ่-เล็ก
.lengthนับจำนวนตัวอักษร.toUpperCase()และ.toLowerCase()ส่งกลับค่าคัดลอกเป็น ตัวพิมพ์ใหญ่ หรือ ตัวพิมพ์เล็ก
console.log("hello".length); // → 5
console.log("hello".toUpperCase()); // → HELLO
Index and slice a string · ใช้ดัชนีและตัดส่วนสตริง
Characters are numbered from 0. A slice takes from start up to — but not including — end. · ตัวอักษรมีเลข_label จาก 0. Slice จะดึงจาก start ไปจนถึง — แต่ไม่รวม — 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.
เข้าถึงข้อมูลภายในสตริง
- แต่ละตัวอักษรมีตำแหน่งนับเริ่มต้นจาก 0:
text[0]คือตำแหน่งแรก .slice(start, end)ดึงส่วนตั้งแต่startขึ้นไป - แต่ไม่รวมถึง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.
ระวัง
- ตำแหน่งเริ่มที่
0ดังนั้นตัวอักษรที่ 4 คือword[3] - เมธอดของสตริงจะสร้าง สตริงใหม่ - ค่าเดิมจะไม่เปลี่ยนแปลง
Common mistakes
- Index from
0;s.lengthis a property (no()). - Strings cannot be changed in place.
ข้อผิดพลาดที่พบบ่อย
- การอ้างอิงตำแหน่งเริ่มที่
0;s.lengthเป็นคุณสมบัติ (ไม่มี()) - ไม่สามารถแก้ไขสตริงในสถานที่เดิมได้
Now you try
- Build and inspect strings.
- Press Check answer to test it.
ลองดูเลย
- สร้างและตรวจสอบสตริง
- กด Check answer เพื่อทดสอบ
let name = "Sam". Use + to log Hello, then the name then !, so it reads Hello, Sam!. · let name = "Sam". ใช้ + เพื่อแสดง Hello, ตามด้วยชื่อแล้วตามด้วย ! เพื่อให้ได้ผลลัพธ์เป็น Hello, Sam!
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Log how many characters are in the string computer using .length (it is 8). · Log จำนวนตัวอักษรในสตริง computer โดยใช้ .length (คือ 8).
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Log the string cat in CAPITALS using .toUpperCase() (it is CAT). · Log สตริง cat เป็น CAPS using .toUpperCase() (คือ CAT).
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
let word = "code". Log just its first letter using word[0] (it is c). · let word = "code". แสดงเพียง ตัวอักษรแรก ของมันโดยใช้ word[0] (ค่าที่ได้คือ c)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่