×

Html & Html5

it training classes Logo

The World's best Software Development Study Portal

Js String


It is the type of an object in which we can store sequence of characters. We have some ways to create the string which are described as follows:-:-

  • By string literal
  • By string object(using new keyword)

By string literal

Syntax

var nameofString = "value of string" ;

Let's take an example:-

<html>

<body>

<script>

   var string = "This is a string"

   document.write(string);



</script>

</body>

</html>

Output of this program

This is one way how we create string.



Through string object by using the new keyword

We use the new keyword to create an array here

Syntax

var nameofString = new String("string literal");

Let's take an example:-

<html>

<body>

<script>

   var stringname =new String("Here is the String"); {

   document.write(stringname);

}

</script>

</body>

</html>

Output of this program

Here is the String

This is other way how we create the string directly by using the new keyword.



Some more methods and description below :-

Methods Description
concat() Combination of two or more string
charAt() provides the char value present at the specified index.
indexOf() provides the position of a char value present in the given string.
lastIndexOf() provides the position of a char value present in the given string by searching a character from the last position.
search() It searches a specified regular expression in a given string and returns its position if a match occurs.
match() It searches a specified regular expression in a given string and returns that regular expression if a match occurs.
replace() It replaces a given string with the specified replacement.
substring() It is used to fetch the part of the given string on the basis of the specified index.
toLowerCase() It converts the given string into lowercase letter.
toUpperCase() It converts the given string into uppercase letter.
toString() It provides a string representing the particular object.
valueOf() It provides the primitive value of string object.






JS training insitute | Best IT Training classes in Gurgaon | Web Designing Training Institute