[Go to site: main page, start]

0% found this document useful (0 votes)
5 views3 pages

JavaScript String Creation & Methods

The document provides an overview of JavaScript strings, explaining how to create strings using string literals and string objects. It also lists various string manipulation methods such as charAt(), concat(), indexOf(), and trim(), along with examples demonstrating their usage. Additionally, it highlights the differences between string methods and their functionalities.

Uploaded by

kamalakar76
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

JavaScript String Creation & Methods

The document provides an overview of JavaScript strings, explaining how to create strings using string literals and string objects. It also lists various string manipulation methods such as charAt(), concat(), indexOf(), and trim(), along with examples demonstrating their usage. Additionally, it highlights the differences between string methods and their functionalities.

Uploaded by

kamalakar76
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript String

The JavaScript string is an object that represents a sequence of characters.


There are 2 ways to create string in JavaScript
 By string literal
 By string object (using new keyword)
1) By string literal
The string literal is created using double quotes.
Syntax: var stringname="string value";
Example
<script>
var str="This is string literal";
[Link](str);
</script>
Output: This is string literal
2) By string object (using new keyword)
The syntax of creating string object using new keyword is given below:
var stringname=new String("string literal");
Here, new keyword is used to create instance of string.
Example
<script>
var stringname=new String("hello javascript string");
[Link](stringname);
</script>
Output: hello javascript string
JavaScript String Methods/String Manipulation methods
Let's see the list of JavaScript string methods with examples.
Methods Description

charAt() It provides the char value present at the specified index.

charCodeAt() It provides the Unicode value of a character present at the specified


index.

concat() It provides a combination of two or more strings.

indexOf() It provides the position of a char value present in the given string.

lastIndexOf() It 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.

1
replace() It replaces a given string with the specified replacement.

substr() It is used to fetch the part of the given string on the basis of the
specified starting position and length.

substring() It is used to fetch the part of the given string on the basis of the
specified index.

slice() It is used to fetch the part of the given string. It allows us to assign
positive as well negative index.

toLowerCase() It converts the given string into lowercase letter.

toLocaleLowerCase() It converts the given string into lowercase letter on the basis of
host?s current locale.

toUpperCase() It converts the given string into uppercase letter.

toLocaleUpperCase() It converts the given string into uppercase letter on the basis of
host?s current locale.

toString() It provides a string representing the particular object.

valueOf() It provides the primitive value of string object.

split() It splits a string into substring array, then returns that newly created
array.

trim() It trims the white space from the left and right side of the string.
Example Programs
1) JavaScript String charAt(index) Method
The JavaScript String charAt() method returns the character at the given index.
<script>
var str="javascript";
[Link]([Link](2));
</script>
Output: v
2) JavaScript String concat(str) Method
The JavaScript String concat(str) method concatenates or joins two strings.
<script>
var s1="javascript ";
var s2="concat example";
var s3=[Link](s2);
[Link](s3);
</script>
Output:
javascript concat example
2
3) JavaScript String indexOf(str) Method
The JavaScript String indexOf(str) method returns the index position of the given string.
<script>
var s1="javascript from javatpoint indexof";
var n=[Link]("from");
[Link](n);
</script>
Output: 11
4) JavaScript String lastIndexOf(str) Method
The JavaScript String lastIndexOf(str) method returns the last index position of the given string.
<script>
var s1="javascript from javatpoint indexof";
var n=[Link]("java");
[Link](n);
</script>
Output: 16
5) JavaScript String toLowerCase() Method
The JavaScript String toLowerCase() method returns the given string in lowercase letters.
<script>
var s1="JavaScript toLowerCase Example";
var s2=[Link]();
[Link](s2);
</script>
Output:javascript tolowercase example
6) JavaScript String toUpperCase() Method
The JavaScript String toUpperCase() method returns the given string in uppercase letters.
<script>
var s1="JavaScript toUpperCase Example";
var s2=[Link]();
[Link](s2);
</script>
Output:JAVASCRIPT TOUPPERCASE EXAMPLE
7) JavaScript String slice(beginIndex, endIndex) Method
The JavaScript String slice(beginIndex, endIndex) method returns the parts of string from given
beginIndex to endIndex. In slice() method, beginIndex is inclusive and endIndex is exclusive.
<script>
var s1="abcdefgh";
var s2=[Link](2,5);
[Link](s2); </script>
Output: cde
8) JavaScript String trim() Method
The JavaScript String trim() method removes leading and trailing whitespaces from the string.
<script>
var s1=" javascript trim "; var s2=[Link]();
[Link](s2); </script>
Output:javascript trim

You might also like