Tag Archives: String

Program JavaScript untuk Mengganti semua Instance Karakter dalam String

Contoh 1: Ganti Semua Contoh Karakter Menggunakan Regex // program to replace all instances of a character in a string const string = ‘Learning JavaScript Program’; const result = string.replace(/a/g, “A”); console.log(result); Keluaran LeArning JAvAScript ProgrAm Dalam contoh di atas, RegEx digunakan dengan replace() metode untuk mengganti semua instance karakter dalam string. /g menyatakan bahwa […]

Program JavaScript untuk Mengkodekan String ke Base64

Base64 adalah sekelompok skema pengkodean biner-ke-teks yang mewakili data biner dalam format string ASCII dengan menerjemahkannya ke dalam representasi radix-64. Contoh 1: Encode String ke Base64 Menggunakan btoa() // program to encode a string to Base64 // defining the string const str = “Learning JavaScript”; // encoding the string const result = window.btoa(str); console.log(result); // […]

Program JavaScript untuk Melakukan Perbandingan String Case Insensitive

Contoh 1: Menggunakan toUpperCase() // program to perform case insensitive string comparison const string1 = ‘JavaScript Program’; const string2 = ‘javascript program’; // compare both strings const result = string1.toUpperCase() === string2.toUpperCase(); if(result) { console.log(‘The strings are similar.’); } else { console.log(‘The strings are not similar.’); } Keluaran The strings are similar. Dalam program di […]

Program JavaScript untuk Memeriksa Apakah String Berisi Substring

Contoh 1: Periksa String dengan include() // program to check if a string contains a substring // take input const str = prompt(‘Enter a string:’); const checkString = prompt(‘Enter a string that you want to check:’); // check if string contains a substring if(str.includes(checkString)) { console.log(`The string contains ${checkString}`); } else { console.log(`The string does […]

Program JavaScript untuk Mengonversi Objek ke String

Contoh 1: Konversi Objek ke String Menggunakan JSON.stringify() // program to convert an object to a string const person = { name: ‘Jack’, age: 27 } const result = JSON.stringify(person); console.log(result); console.log(typeof result); Keluaran {“name”:”Jack”,”age”:27} string Dalam contoh di atas, JSON.stringify() metode yang digunakan untuk mengubah objek menjadi string. Itu typeof operator memberikan tipe data […]

Program JavaScript untuk Memangkas String

Contoh 1: Potong String // program to trim a string const string = ‘ Hello World ‘; const result = string.trim(); console.log(result); Keluaran Hello World Dalam contoh di atas, trim() metode yang digunakan untuk memotong string. Itu trim() metode menghilangkan spasi putih dari kedua sisi string. Contoh 2: Memangkas String Menggunakan RegEx // program to […]

Program JavaScript untuk Memeriksa apakah sebuah String Dimulai Dengan String Lain

Contoh 1: Menggunakan startWith() // program to check if a string starts with another string const string = ‘hello world’; const toCheckString = ‘he’; if(string.startsWith(toCheckString)) { console.warn(‘The string starts with “he”.’); } else { console.warn(`The string does not starts with “he”.`); } Keluaran The string starts with “he”. Pada program di atas, startsWith() metode yang […]

Program JavaScript untuk Menghasilkan String Acak

Contoh 1: Hasilkan String Acak // program to generate random strings // declare all characters const characters =’ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789′; function generateString(length) { let result = ‘ ‘; const charactersLength = characters.length; for ( let i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } console.log(generateString(5)); Keluaran B5cgH Dalam […]

Program JavaScript untuk Memformat Angka sebagai String Mata Uang

Contoh 1: Memformat Angka sebagai String Mata Uang // program to format numbers as currency string const formatter = new Intl.NumberFormat(‘en-US’, { style: ‘currency’, currency: ‘USD’ }); formatter.format(2500); Keluaran $2,500.00 Dalam program di atas, kami telah menggunakan Intl.NumberFormat obyek. Itu Intl.NumberFormat objek memungkinkan pemformatan angka peka bahasa. Contoh 2: Memformat Angka sebagai String Mata Uang […]

Program JavaScript untuk Membuat String Multiline

Contoh 1: Buat String Multiline Menggunakan + // program to create a multiline strings // using the + operator const message = ‘This is a long messagen’ + ‘that spans across multiple linesn’ + ‘in the code.’ console.log(message); Keluaran This is a long message that spans across multiple lines in the code. Dalam contoh di […]