31
All JS String Methods In One Post!
Hello everybody, I'm Aya Bouchiha, on this beautiful day we're going to discuss all string methods in
Javascript!
Javascript!
Firstly, we need to know that all All methods do not change the original string, they return a new one.
const firstName = 'Aya ';
const lastName = 'Bouchiha';
// 3 methods to concatenate two strings
console.log(firstName.concat(lastName)); // Aya Bouchiha
console.log(firstName + lastName); // Aya Bouchiha
console.log(`${firstName}${lastName}`); // Aya Bouchiha
const quote = "If you don't know where you are going, any road will get you there.";
console.log(quote.match(/you/g)) // [ "you", "you", "you" ]
const conversation = `Hi, I'm Aya Bouchiha\nHello, I'm John Doe, nice to meet you.`;
const matchedArrays = [...conversation.matchAll(/I'm\s(?<firstName>[a-zA-Z]+)\s(?<lastName>[a-zA-Z]+)/gi)];
console.log(matchedArrays[0])
for (let matchedArray of matchedArrays) {
const {firstName, lastName} = matchedArray['groups']
console.log(firstName, lastName)
}
Output:
[
"I'm Aya Bouchiha",
'Aya',
'Bouchiha',
index: 4,
input: "Hi, I'm Aya Bouchiha\nHello, I'm John Doe, nice to meet you.",
groups: [Object: null prototype] { firstName: 'Aya', lastName: 'Bouchiha' }
]
Aya Bouchiha
John Doe
const allLetters = 'abcdefghijklmnopqrstuvwxyz';
console.log(allLetters.split())
console.log(allLetters.split(''))
const emails = 'developer.aya.b@gmail.com,johndoes@gmail.com,simon.dev@gmail.com';
console.log(emails.split(','))
Output:
[ 'abcdefghijklmnopqrstuvwxyz' ]
[
'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x',
'y', 'z'
]
[
'developer.aya.b@gmail.com',
'johndoes@gmail.com',
'simon.dev@gmail.com'
]
const email = 'john.doe@gmail.com';
console.log(email.replace('@gmail.com', '')); // john.doe
console.log(email.replace(/@[a-z]+.[a-z]+/g, '')); // john.doe
const slug = '5-html-tags-that-almost-nobody-knows';
// 5 html tags that almost nobody knows
console.log(slug.replaceAll('-', ' '));
// 5 html tags that almost nobody knows
console.log(slug.replaceAll(/-/g, ' '));
const quote = 'A dream does not become reality through magic; it takes sweat, determination, and hard work';
console.log(quote.search('magic')); // 40
console.log(quote.search(/life/g)); // -1
const inputValue = ' Aya Bouchiha\t';
console.log(inputValue.trim()); // Aya Bouchiha
const address = 'Morocco, Rabat';
console.log(address.includes('Morocco'));// true
console.log(address.includes('morocco'));// false
console.log(address.includes('tanger')); // false
const name = 'AYa BoUCHIha';
console.log(name.toLowerCase()) // aya bouchiha
const name = 'AYa BoUCHIha';
console.log(name.toUpperCase()) // AYA BOUCHIHA
const turkishSentence = 'iskender kebap';
// ISKENDER KEBAP
console.log(turkishSentence.toLocaleUpperCase('en-us'));
// İSKENDER KEBAP
console.log(turkishSentence.toLocaleUpperCase('tr'))
const firstName = 'aya';
console.log(firstName.repeat(3)) // ayaayaaya
const fullName = 'Aya Bouchiha';
console.log(fullName.slice()) // Aya Bouchiha
console.log(fullName.slice(0,3)) // Aya
console.log(fullName.slice(4,fullName.length)) // Bouchiha
const fullName = 'Aya Bouchiha';
console.log(fullName.substr(0,3)) // Aya
console.log(fullName.substr(4,8)) // Bouchiha
0 <= index < string.length
const product = 'laptop';
console.log(product.charAt(3)) // t
console.log(product.charAt(10)) // ''
product.charAt("this is a string!") // l
console.log(product.charAt()) // l
const product = 'laptop';
console.log(`the character code of ${product.charAt(2)} is ${product.charCodeAt(2)}`)
// the character code of p is 112
const phoneNumber = '+212612342187';
console.log(phoneNumber.startsWith('+212')) // true
console.log(phoneNumber.startsWith('6',4)) // true
console.log(phoneNumber.startsWith('6',3)) // false
const address = 'tanger, Morocco';
console.log(address.endsWith('Morocco')); // true
console.log(address.endsWith('Canada')); // false
const gmail = 'developer.aya.b@gmail.com';
const isGmail = gmail.endsWith('@gmail', gmail.length - 4)
console.log(isGmail); // true
console.log(String.fromCharCode(112)) // p
console.log(String.fromCharCode(105,106)) // ij
const quote = "every day may not be good... but there's something good in every day";
console.log(quote.indexOf('good')); // 21
console.log(quote.indexOf('good',24)); // 51
const quote = "every day may not be good... but there's something good in every day";
console.log(quote.lastIndexOf('good')); // 51
console.log(quote.lastIndexOf('good',24)); // 21
const word1 = 'feel';
const word2 = 'flee';
// returns -1
// because word1 comes before word2
console.log(word1.localeCompare(word2))
const fName = new String('Aya');
const lName = 'Bouchiha';
console.log(fName); // [String: 'Aya']
console.log(fName.valueOf()); // Aya
console.log(lName.valueOf()); // Bouchiha
const moroccanCity = new String('tanger');
console.log(moroccanCity); // [String: 'tanger']
console.log(moroccanCity.toString()) // tanger
to contact me:
Have an amazing day!
31