Sunday, October 7, 2018

Learn Javascript String examples | Typescript String Examples

In this blog post, We are going to learn Frequently used String examples in javascript/typescript.
String examples in typescript and javascript

How to make the first letter of a string as an upper case?

In some cases, We need to display the String word in uppercase This example cover the capitalize the first character of a string

function captialFirstLetterString(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}
console.log(captialFirstLetterString('this is test'))
Output is

How to capitalize the first letter of every word of a string?

function captialFirstLetterEveryWordString(string) {
    return string.toLowerCase()
    .split(' ')
    .map((s) => s.charAt(0).toUpperCase() + s.substring(1))
    .join(' ');
}
var text = "Newyork city test example";
console.log(captialFirstLetterEveryWordString(text))
Output is
Newyork City Test Example

How to find the String size in bytes 

Using Blob object, pass string object which returns the size of bytes of a string
console.log(new Blob(['kiranbabu']).size) //9

String toLowerCase example 

toLowerCase() method of a string object converts the string in lowercase

console.log(ConvertUppercase('Kiran')) // kiran 
function ConvertUppercase(str) {
    return str.toLowerCase()
}

String toUpperCase example 

toUpperCase() method of a string object converts the string in uppercase

console.log(ConvertUppercase('kiran')) // KIRAN
function ConvertUppercase(str) {
    return str.toUpperCase()
}

Related article


EmoticonEmoticon