senarai kaedah rentetan JavaScript yang paling biasa

Linda Hamilton
Lepaskan: 2024-11-19 19:22:03
asal
638 orang telah melayarinya

list of the most common JavaScript string methods

senarai kaedah rentetan JavaScript yang paling biasa:

  • panjang:
let message = "Hello, world!";
console.log(message.length); // Output: 13
Salin selepas log masuk
  • concat():
let greeting = "Hello";
let name = "Alice";
let fullGreeting = greeting.concat(", ", name, "!");
console.log(fullGreeting); // Output: "Hello, Alice!"
Salin selepas log masuk
  • charAt():
let message = "JavaScript";
console.log(message.charAt(0)); // Output: "J"
console.log(message.charAt(5)); // Output: "S"
Salin selepas log masuk
  • indexOf():
let sentence = "The quick brown fox jumps over the lazy dog.";
console.log(sentence.indexOf("quick")); // Output: 4
console.log(sentence.indexOf("dog")); // Output: 37
console.log(sentence.indexOf("lion")); // Output: -1 (not found)
Salin selepas log masuk
  • lastIndexOf():
let quote = "To be, or not to be, that is the question.";
console.log(quote.lastIndexOf("be")); // Output: 23
console.log(quote.lastIndexOf("the")); // Output: 33
Salin selepas log masuk
  • keping():
let fruit = "apple, banana, cherry";
console.log(fruit.slice(0, 5)); // Output: "apple"
console.log(fruit.slice(7, 13)); // Output: "banana"
console.log(fruit.slice(15)); // Output: "cherry"
Salin selepas log masuk
  • subrentetan():
let text = "Hello, world!";
console.log(text.substring(0, 5)); // Output: "Hello"
console.log(text.substring(7, 12)); // Output: "world"
Salin selepas log masuk
  • substr():
let message = "The quick brown fox jumps.";
console.log(message.substr(4, 5)); // Output: "quick"
console.log(message.substr(10)); // Output: "brown fox jumps."
Salin selepas log masuk
  • ke Huruf Kecil():
let text = "JAVASCRIPT IS AWESOME!";
console.log(text.toLowerCase()); // Output: "javascript is awesome!"
Salin selepas log masuk
  • kepadaUpperCase():
let text = "hello, world!";
console.log(text.toUpperCase()); // Output: "HELLO, WORLD!"
Salin selepas log masuk
  • trim():
let greeting = "   Hello, world!   ";
console.log(greeting.trim()); // Output: "Hello, world!"
Salin selepas log masuk
  • ganti():
let text = "I like cats. Cats are fluffy.";
console.log(text.replace("cats", "dogs")); // Output: "I like dogs. Cats are fluffy."
console.log(text.replace(/cats/i, "dogs")); // Output: "I like dogs. Cats are fluffy." (case-insensitive)
Salin selepas log masuk
  • pecah():
let csv = "apple,banana,cherry";
let fruits = csv.split(",");
console.log(fruits); // Output: ["apple", "banana", "cherry"]
Salin selepas log masuk
  • termasuk():
let message = "The quick brown fox jumps over the lazy dog.";
console.log(message.includes("quick")); // Output: true
console.log(message.includes("tiger")); // Output: false
Salin selepas log masuk
  • bermulaDengan():
let greeting = "Hello, world!";
console.log(greeting.startsWith("Hello")); // Output: true
console.log(greeting.startsWith("world")); // Output: false
Salin selepas log masuk
  • berakhirDengan():
let filename = "myfile.pdf";
console.log(filename.endsWith(".pdf")); // Output: true
console.log(filename.endsWith(".txt")); // Output: false
Salin selepas log masuk
  • perlawanan():
let text = "The quick brown fox jumps.";
let pattern = /quick/;
console.log(text.match(pattern)); // Output: ["quick"]
Salin selepas log masuk
  • carian():
let text = "The quick brown fox jumps.";
let pattern = /brown/;
console.log(text.search(pattern)); // Output: 10
Salin selepas log masuk
  • ulang():
let message = "Hello ";
console.log(message.repeat(3)); // Output: "Hello Hello Hello "
Salin selepas log masuk
  • trimStart():
let greeting = "   Hello, world!   ";
console.log(greeting.trimStart()); // Output: "Hello, world!   "
Salin selepas log masuk
  • trimEnd():
let greeting = "   Hello, world!   ";
console.log(greeting.trimEnd()); // Output: "   Hello, world!"
Salin selepas log masuk
  • padStart():
let number = "42";
console.log(number.padStart(4, "0")); // Output: "0042"
Salin selepas log masuk
  • padEnd():
    let word = "JavaScript";
    console.log(word.padEnd(15, ".")); // Output: "JavaScript....."

  • splice():

let str = "Hello, world!";
let result = str.splice(7, 5, "everyone");
console.log(result); // Output: "world"
console.log(str); // Output: "Hello, everyone!"
Salin selepas log masuk
  • kepadaString():
let num = 42;
console.log(num.toString()); // Output: "42"
Salin selepas log masuk
  • valueOf():
let str = "JavaScript";
console.log(str.valueOf()); // Output: "JavaScript"
Salin selepas log masuk
  • charCodeAt():
let message = "Hello";
console.log(message.charCodeAt(0)); // Output: 72
console.log(message.charCodeAt(4)); // Output: 111
Salin selepas log masuk
  • codePointAt():
let emoji = "?";
console.log(emoji.codePointAt(0)); // Output: 128512
Salin selepas log masuk

normalkan():

let str1 = "café";
let str2 = "cafe\u0301";
console.log(str1.normalize() === str2.normalize()); // Output: true
Salin selepas log masuk

Ini merangkumi pelbagai tugas manipulasi rentetan dalam JavaScript, daripada operasi asas seperti mendapatkan panjang atau rentetan gabungan kepada teknik yang lebih maju seperti padanan corak dan penormalan Unikod.

Atas ialah kandungan terperinci senarai kaedah rentetan JavaScript yang paling biasa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan