Learn Javascript String examples | Typescript
- Admin
- Mar 10, 2024
- Javascript
In this blog post, learn Frequently used String examples in javascript/typescript.
You can check my other post on Javascript check substring exists in a String
How to make the first letter of a string an upper case in javascript
In some cases, We need to display the String word in uppercase. Its cover capitalizes 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 in javascript?
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 in javascript
Using the Blob object, pass the string object which returns the size of bytes of a string.
console.log(new Blob(["abcdefghi"]).size); //9
String toLowerCase example in javascript
toLowerCase() method of a string object converts the string into lowercase
console.log(ConvertLowercase("Abc")); // abc
function ConvertLowercase(str) {
return str.toLowerCase();
}
String toUpperCase example in javascript
toUpperCase() method of a string object converts the string in uppercase
console.log(ConvertUppercase("kiran")); // KIRAN
function ConvertUppercase(str) {
return str.toUpperCase();
}