Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 18, 2022 05:38 am GMT

W3Schools Strings Method With Example

Here are Strings Method that I learned on W3schools
you can copy paste it and check the output and experiment with it

/*IN JAVASCRIPTstrings are primitve values and they dont have mehthod or propertiesBUTJS consider stings as objects so  we can use mehthod and properties with Srings*/let text = "Hello World Hello";//returns the lenght of the textconsole.log(text.length);//extracting the strings from indeconsole.log(text.slice(2,5));//extracting the string if its negative then starts from backconsole.log(text.slice(-1,));//extractin the string : (from, no of amount to extract);console.log(text.substr(2, 6));//replace the specific Worldconsole.log(text.replace("Hello","Bye"));/*if we have text as >>>>> "Hellow my world you are hello":  text.replace("Hello","bye"); >> will replace only 1st hello: to replace all 'Hello' we use : text.replace("/Hello/g","bye");*/console.log(text.replace("/Hello/g","bye"));// or we can useconsole.log(text.replaceAll("Hellow", "bye"));//to upper caseconsole.log(text.toUpperCase());// to lower caseconsole.log(text.toLowerCase());// now its time to trim soconsole.log("TRIMMING  " +  text.trim());// adding some before num//if its nume first we need to convet it into stringslet padnum = "hey bitch";let convert = padnum.toString();let padstart = convert.padStart(10,"o");console.log(padstart);//SAme you can do with pad endvar name = "bhag";let outputofname = name.padEnd(50," bhai ");console.log(outputofname);//Extracting String Character at specific positionconsole.log(text.charAt(9));//Extracting String Character code at specific positionconsole.log(text.charCodeAt(9));// you can also do thisconsole.log(text[9]);//Converting String to arrays var convertedtoarray = text.split(" ");console.log(convertedtoarray[2]);

Original Link: https://dev.to/developedbyjk/w3schools-strings-method-with-example-3ca4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To