String
សេចក្តីផ្តើម
String គឺជា primitive data type ក្នុង JavaScript សម្រាប់ទំនាក់ទំនងជាមួយអក្សរ និងអត្ថបទ។ String អាចមានតួអក្សរ ចំនួន ឬនិមិត្តសញ្ញាណាមួយ។
បង្កើត String
វិធី | ឧទាហរណ៍ | ការពិពណ៌នា |
---|---|---|
Single quotes | 'Hello' | ស្រួលប្រើបំផុត |
Double quotes | "Hello" | ស្រដៀងគ្នានឹង single quotes |
Template literals | `Hello ${name}` | អាចបញ្ចូល variable |
String Properties
let text = "Hello World";
text.length; // 11
Property length
ប្រើសម្រាប់ដឹងចំនួនតួអក្សរ
String Methods - Search
let text = "Hello World";
text.indexOf("World"); // 6
text.lastIndexOf("o"); // 7
text.search("World"); // 6
text.includes("Hello"); // true
text.startsWith("Hello"); // true
text.endsWith("World"); // true
Methods សម្រាប់ស្វែងរកអក្សរ
String Methods - Extract
let text = "Hello World";
text.slice(0, 5); // "Hello"
text.substring(6, 11); // "World"
text.substr(6, 5); // "World"
text.charAt(0); // "H"
text[0]; // "H"
Methods សម្រាប់ទាញយកផ្នែកខ្លះ
String Methods - Replace
let text = "Hello World";
text.replace("World", "JavaScript"); // "Hello JavaScript"
text.replaceAll("o", "0"); // "Hell0 W0rld"
Methods សម្រាប់ជំនួស
String Methods - Case
let text = "Hello World";
text.toUpperCase(); // "HELLO WORLD"
text.toLowerCase(); // "hello world"
Methods សម្រាប់ប្តូរទៅធំ/តូច
String Methods - Trim & Split
let text = " Hello World ";
text.trim(); // "Hello World"
text.trimStart(); // "Hello World "
text.trimEnd(); // " Hello World"
let words = "a,b,c".split(","); // ["a", "b", "c"]
Methods សម្រាប់កាត់ និងបំបែក
Best Practices
- ប្រើ template literals សម្រាប់ string concatenation
- ប្រើ
===
សម្រាប់ប្រៀបធៀប string - String គឺជា immutable (មិនអាចផ្លាស់ប្តូរបាន)
- ប្រើ
trim()
មុនពេលពិនិត្យ input
Interactive Examples
- String Length Calculator
- String Search Demo
- String Extract Demo
- String Replace Demo
- String Case Converter
- String Split Demo
សង្ខេប
- String គឺជា primitive data type សម្រាប់អត្ថបទ
- Property: length
- Search methods: indexOf, includes, startsWith, endsWith
- Extract methods: slice, substring, charAt
- Modify methods: replace, toUpperCase, toLowerCase, trim, split