Strings are use to store and manipulate. It is a group of Characters is called String. String can be creates using single quotes and double quotes. It can be create using the following Syntax.

Home Page - Basic Engineer
Home Page – Basic Engineer

let name = “basic”

Template Literals

Template Literals use backticks of Quotes to define a string.

Let name = `basic`

With template literals, it is possible to use both single as well as double quotes inside a string.

let Sentence = ` my name “is” Basic engineer.

We can insert variable directly in template literal. This is called String interpolation.

let a = ` this is $ {name}`

<html>
<body>
<script>  
var why="hello basic phone";  
document.write(why);  
</script>
</body>
</html>

Output : hello basic phone.

String Methods

JavaScript String Methods

charAt()

This function returns the given Position character is knows charAt().

Syntax : Obj.charAt(index);

Example

<html>
<body>
<script>  
var why="basic";  
document.write(why.charAt(2));  
</script>  
</body>
</html>

Output: s

charCodeAt()

The Unicode value of the character at the given index.

Concat()

This function concat two string and store it in third one is knows as Concat.

Syntax: str1.concat(str2);

Example

<html>
<body>
<script>  
var su1="hello ";  
var su2="student";  
var su3=su1+su2;  
document.write(su3);  
</script>  
</body>
</html>

Output: hello student

IndexOf()

The position of a char value present in the given string is know as IndexOf().

Example

<html>
<body>
<script>  
var su1="The position of a char value";  
var nm=su1.indexOf("a");  
document.write(nm);  
</script>  
</body>
</html>

Output : 16

lastIndexOf()

String object of the last occurrence of the specified value is knows lastIndexOf().

Example

<html>
<body>
<script>  
var sn="String object of the last occurrence";  
var nm=sn.lastIndexOf("of");  
document.write(nm);  
</script>  
</body>
</html>

Output : 14

replace()

This method replace all the occurrence of given character with new character is knows as replace().

syntax : obj2= obj1 replace(‘old char’ , ‘new char’);

example

<html>
<body>
<script>
    var string = 'why';
    var newstring = string.replace(/why/, 'what');
 
    document.write(newstring);
</script>
</body>
</html>

Output : what

Slice()

example

<html>
<body>
<script>  
var what="why should hire you";  
var why=what.slice(4,16);  
document.write(why);  
</script>  
</body>
</html>

Output: should hire

toLowerCase()

Convert the string into lowercase is knows as toLowerCase

Syntax : obj.toLowerCase();

Example

<html>
<body>
<script>  
var su1="WHAT IS YOUR NAME";  
var su2=su1.toLowerCase();  
document.write(su2);  
</script>  
</body>
</html>

Output : what is your name

toUpperCase()

Convert the string into uppercase is knows as toUpperCase().

Syntax: obj.toUpperCase();

Example

<html>
<body>
<script>  
var why="what is your name";  
var whose=why.toUpperCase();  
document.write(whose);  
</script>  
</body>
</html>

Output : WHAT IS YOUR NAME

trim()

It removes the blanks space from left and right side of the given string is knows as trim().

Syntax : obj2=obj1.trim();

<html>
<body>
<script>  
var sum1="              superman badman";  
var sum2=sum1.trim();  
document.write(sum2);  
</script>  
</body>
</html>

Output: Superman badman

split()

Example

<html>
<body>
<script>
var str="why should hire you";  
document.write(str.split(" "));  
</script>    
</body>
</html>

Output: why,should,hire,you

search()

valueOf()

lenght()

This return the length of the string including space. Length of the string means number of characters.

syntax: obj.lenght();

equal()

This returns true it given both strings are identical.

Syntax: obj1.equal (obj) ;

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JavaScripts String please check Wikipedia Click here

Stay Connected Stay Safe. Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *