SlideShare a Scribd company logo
Javascript built in String Functions
Content
• String Functions
Covered String Functions
• charAt()
• concat()
• indexOf()
• lastIndexOf()
• replace()
• search()
• substr()
• substring()
• toLowerCase()
• toUppserCase()
charAt()
Description:
• This method returns the character from the
specified index. Characters in a string are indexed
from left to right.
• The index of the first character is 0, and the index
of the last character in a string called string Name
is stringName.length - 1.
Syntax:
string.charAt(index);
charAt
Example
<html>
<head>
<title>JavaScript String charAt() Method</title>
</head>
<body>
<script type="text/JavaScript">
var str = new String( "This is string" );
document.writeln("str.charAt(0) is:" + str.charAt(0));
</script>
</body>
</html>
Output
str.charAt(0) is: T
concat()
Description:
• This method adds two or more strings and returns a new
single string.
Syntax:
string.concat(string2, string3[, ..., stringN]);
parameters:
string2...stringN : These are the strings to be concatenated.
concat()
Example:
<html>
<head>
<title>JavaScript String concat() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String( "This is string one" );
var str2 = new String( "This is string two" );
var str3 = str1.concat( str2 );
document.write("Concatenated String :" + str3);
</script>
</body></html>
Output:
Concatenated String :This is string oneThis is string two.
indexOf
Description:
• the first occurrence of the specified value, starting the
search at This method returns the index within the calling
String object of fromIndex or -1 if the value is not found.
Syntax:
string.indexOf(searchValue[, fromIndex])
Parameters:
searchValue : A string representing the value to search for.
fromIndex : The location within the calling string to start the
search from. It can be any integer between 0 and the length
of the string. The default value is 0.
indexOf
Example:
<html>
<head>
<title>JavaScript String indexOf() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String( "This is string one" );
var index = str1.indexOf( "string" );
document.write("indexOf found String :" + index );
document.write("<br />");
var index = str1.indexOf( "one" );
document.write("indexOf found String :" + index );
</script>
</body></html>
Oputput:
indexOf found String :8
indexOf found String :15
lastIndexOf()
Description:
– This method returns the index within the calling String
object of the last occurrence of the specified value, starting
the search at fromIndex or -1 if the value is not found.
Syntax:
– string.lastIndexOf(searchValue[, fromIndex])
Parameters:
– searchValue : A string representing the value to search for.
– fromIndex : The location within the calling string to start the
search from. It can be any integer between 0 and the length
of the string. The default value is 0.
Return Value:
– . Returns the index of the last found occurrence otherwise -1
if not found
lastIndexOf()
Example:
<html>
<head>
<title>JavaScript String lastIndexOf() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String( "This is string one and again string" );
var index = str1.lastIndexOf( "string" );
document.write("lastIndexOf found String :" + index );
document.write("<br />");
var index = str1.lastIndexOf( "one" );
document.write("lastIndexOf found String :" + index );
</script>
</body>
</html>
Output:
lastIndexOf found String :29
lastIndexOf found String :15
replace()
Description:
This method finds a match between a regular expression
and a string, and replaces the matched substring with a new
substring.
The replacement string can include the following special
replacement patterns:
Syntax:
string.replace(regexp/substr, newSubStr/function[, flags]);
replace()
Parameters:
– regexp : A RegExp object. The match is replaced by the
return value of parameter #2.
– substr : A String that is to be replaced by newSubStr.
– newSubStr : The String that replaces the substring received
from parameter #1.
– function : A function to be invoked to create the new
substring.
– flags : A String containing any combination of the RegExp
flags: g - global match, i - ignore case, m - match over
multiple lines. This parameter is only used if the first
parameter is a string.
Return Value:
– It simply returns a new changed string.
replace()
Example:
Following example shows how to use the global and ignore case flags
which permits replace to replace each occurrence of 'apples' in the string
with 'oranges'.
<html>
<head
<title>JavaScript String replace() Method</title>
</head>
<body>
<script type="text/javascript">
var re = /apples/gi;
var str = "Apples are round, and apples are juicy.";
var newstr = str.replace(re, "oranges");
document.write(newstr );
</script>
</body>
</html>
search()
Description:
– This method Executes the search for a match between a
regular expression and this String object.
Syntax:
– string.search(regexp);
Parameters:
– regexp : A regular expression object. If a non-RegExp object
obj is passed, it is implicitly converted to a RegExp by using
new RegExp(obj)
Return Value:
– If successful, search returns the index of the regular
expression inside the string. Otherwise, it returns -1.
search()
Example:
<html>
<head>
<title>JavaScript String search() Method</title>
</head>
<body>
<script type="text/javascript">
var re = /apples/gi;
var str = "Apples are round, and apples are juicy.";
if ( str.search(re) == -1 ){
document.write("Does not contain Apples" );
}else{
document.write("Contains Apples" );
}
</script>
</body>
</html>
Output:
Contains Apples
substr()
Description:
– This method returns the characters in a string beginning at
the specified location through the specified number of
characters.
Syntax:
– string.substr(start[, length]);
Parameters:
– start : Location at which to begin extracting characters (an
integer between 0 and one less than the length of the string).
– length : The number of characters to extract.
Note: If start is negative, substr uses it as a character index
from the end of the string.
Return Value:
– The substr method returns the new sub string based on
given parameters.
substr()
Example:
<html>
<head><title>JavaScript String substr() Method</title></head><body>
<script type="text/javascript">
var str = "Apples are round, and apples are juicy.";
document.write("(1,2): " + str.substr(1,2));
document.write("<br />(-2,2): " + str.substr(-2,2));
document.write("<br />(1): " + str.substr(1));
document.write("<br />(-20, 2): " + str.substr(-20,2));
document.write("<br />(20, 2): " str.substr(20,2));
</script></body></html>
Output:
(1,2): pp
(-2,2): Ap
(1): pples are round, and apples are juicy.
(-20, 2): Ap
(20, 2): d
substring()
Description:
– This method returns a subset of a String object.
Syntax:
– string.substring(indexA, [indexB])
Parameters:
– indexA : An integer between 0 and one less than the length of
the string.
– indexB : (optional) An integer between 0 and the length of the
string.
Return Value:
– The substring method returns the new sub string based on
given parameters
substring()
Example:
<html>
<head><title>JavaScript String substring()
Method</title></head><body>
<script type=”text/javascript”>
var str = “Apples are round, and apples are juicy.”;
document.write( “(1,2): “ + str.substring(1,2));
document.write( “<br />(0,10):” + str.substring(0, 10));
document.write(“<br />(5): “ + str.substring(5));
</script></body> </html>
Output:
(1,2): p(0,10): Apples are (5): s are round, and apples are juicy.
toLowerCase()
Description:
– This method returns the calling string value
converted to lowercase.
Syntax:
– string.toLocaleLowerCase( )
Return Value:
– Returns the calling string value converted to
lowercase.
toLowerCase()
Example:
<html>
<head>
<title>JavaScript String toLowerCase() Method
</title></head><body>
<script type="text/javascript">
var str = "Apples are round, and Apples are Juicy.";
document.write(str.toLowerCase( ));
</script></body> </html>
Output:
apples are round, and apples are juicy.
toUpperCase()
Description:
– This method returns the calling string value converted to
uppercase.
Syntax:
– string.toUpperCase( )
Return Value:
– Returns a string representing the specified object.
toUpperCase()
Example:
<html>
<head>
<title>JavaScript String toUpperCase() Method</title>
</head>
<body>
<script type="text/javascript">
var str = "Apples are round, and Apples are Juicy.";
document.write(str.toUpperCase( ));
</script>
</body>
</html>
Output:
APPLES ARE ROUND, AND APPLES ARE JUICY.
Javascript built in String Functions

More Related Content

What's hot (20)

PPTX
Regular expressions in Python
Sujith Kumar
 
PPTX
OOPs in Java
Ranjith Sekar
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PPTX
C# Constructors
Prem Kumar Badri
 
PPSX
Javascript variables and datatypes
Varun C M
 
PDF
Python programming : Files
Emertxe Information Technologies Pvt Ltd
 
PPT
Introduction to Javascript
Amit Tyagi
 
PPTX
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
ODP
Multithreading In Java
parag
 
PDF
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
 
PPT
JavaScript Functions
Reem Alattas
 
PDF
javascript objects
Vijay Kalyan
 
PPTX
Regular Expressions in Java
OblivionWalker
 
PDF
JavaScript: Variables and Functions
Jussi Pohjolainen
 
PPTX
L21 io streams
teach4uin
 
PPTX
Operators and Expressions in Java
Abhilash Nair
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPT
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPTX
Jquery Complete Presentation along with Javascript Basics
EPAM Systems
 
Regular expressions in Python
Sujith Kumar
 
OOPs in Java
Ranjith Sekar
 
jQuery for beginners
Arulmurugan Rajaraman
 
C# Constructors
Prem Kumar Badri
 
Javascript variables and datatypes
Varun C M
 
Python programming : Files
Emertxe Information Technologies Pvt Ltd
 
Introduction to Javascript
Amit Tyagi
 
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Multithreading In Java
parag
 
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
 
JavaScript Functions
Reem Alattas
 
javascript objects
Vijay Kalyan
 
Regular Expressions in Java
OblivionWalker
 
JavaScript: Variables and Functions
Jussi Pohjolainen
 
L21 io streams
teach4uin
 
Operators and Expressions in Java
Abhilash Nair
 
Classes objects in java
Madishetty Prathibha
 
PHP - Introduction to PHP AJAX
Vibrant Technologies & Computers
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Jquery Complete Presentation along with Javascript Basics
EPAM Systems
 

Similar to Javascript built in String Functions (20)

PPTX
Javascripting.pptx
Vinod Srivastava
 
PPT
JavaScript Objects
Reem Alattas
 
PPTX
Object Oriented Programming Using C++: C++ STL Programming.pptx
RashidFaridChishti
 
PPTX
Java script arrays
Frayosh Wadia
 
PPTX
Java script arrays
Frayosh Wadia
 
PPT
Strings
naslin prestilda
 
DOCX
WD programs descriptions.docx
anjani pavan kumar
 
PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
PPTX
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
PPTX
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PPTX
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
PPTX
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
PPTX
11. session 11 functions and objects
Phúc Đỗ
 
PPT
9781305078444 ppt ch08
Terry Yoast
 
DOCX
Unitii string
Sowri Rajan
 
ODP
PHP Web Programming
Muthuselvam RS
 
PPTX
13 Strings and Text Processing
Intro C# Book
 
PPTX
What is new in Java 8
Sandeep Kr. Singh
 
PPTX
Java String Handling
Infoviaan Technologies
 
PDF
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
Javascripting.pptx
Vinod Srivastava
 
JavaScript Objects
Reem Alattas
 
Object Oriented Programming Using C++: C++ STL Programming.pptx
RashidFaridChishti
 
Java script arrays
Frayosh Wadia
 
Java script arrays
Frayosh Wadia
 
WD programs descriptions.docx
anjani pavan kumar
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
11. session 11 functions and objects
Phúc Đỗ
 
9781305078444 ppt ch08
Terry Yoast
 
Unitii string
Sowri Rajan
 
PHP Web Programming
Muthuselvam RS
 
13 Strings and Text Processing
Intro C# Book
 
What is new in Java 8
Sandeep Kr. Singh
 
Java String Handling
Infoviaan Technologies
 
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
Ad

Recently uploaded (20)

PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
infertility, types,causes, impact, and management
Ritu480198
 
Controller Request and Response in Odoo18
Celine George
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Horarios de distribución de agua en julio
pegazohn1978
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
epi editorial commitee meeting presentation
MIPLM
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
Ad

Javascript built in String Functions

  • 3. Covered String Functions • charAt() • concat() • indexOf() • lastIndexOf() • replace() • search() • substr() • substring() • toLowerCase() • toUppserCase()
  • 4. charAt() Description: • This method returns the character from the specified index. Characters in a string are indexed from left to right. • The index of the first character is 0, and the index of the last character in a string called string Name is stringName.length - 1. Syntax: string.charAt(index);
  • 5. charAt Example <html> <head> <title>JavaScript String charAt() Method</title> </head> <body> <script type="text/JavaScript"> var str = new String( "This is string" ); document.writeln("str.charAt(0) is:" + str.charAt(0)); </script> </body> </html> Output str.charAt(0) is: T
  • 6. concat() Description: • This method adds two or more strings and returns a new single string. Syntax: string.concat(string2, string3[, ..., stringN]); parameters: string2...stringN : These are the strings to be concatenated.
  • 7. concat() Example: <html> <head> <title>JavaScript String concat() Method</title> </head> <body> <script type="text/javascript"> var str1 = new String( "This is string one" ); var str2 = new String( "This is string two" ); var str3 = str1.concat( str2 ); document.write("Concatenated String :" + str3); </script> </body></html> Output: Concatenated String :This is string oneThis is string two.
  • 8. indexOf Description: • the first occurrence of the specified value, starting the search at This method returns the index within the calling String object of fromIndex or -1 if the value is not found. Syntax: string.indexOf(searchValue[, fromIndex]) Parameters: searchValue : A string representing the value to search for. fromIndex : The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.
  • 9. indexOf Example: <html> <head> <title>JavaScript String indexOf() Method</title> </head> <body> <script type="text/javascript"> var str1 = new String( "This is string one" ); var index = str1.indexOf( "string" ); document.write("indexOf found String :" + index ); document.write("<br />"); var index = str1.indexOf( "one" ); document.write("indexOf found String :" + index ); </script> </body></html> Oputput: indexOf found String :8 indexOf found String :15
  • 10. lastIndexOf() Description: – This method returns the index within the calling String object of the last occurrence of the specified value, starting the search at fromIndex or -1 if the value is not found. Syntax: – string.lastIndexOf(searchValue[, fromIndex]) Parameters: – searchValue : A string representing the value to search for. – fromIndex : The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0. Return Value: – . Returns the index of the last found occurrence otherwise -1 if not found
  • 11. lastIndexOf() Example: <html> <head> <title>JavaScript String lastIndexOf() Method</title> </head> <body> <script type="text/javascript"> var str1 = new String( "This is string one and again string" ); var index = str1.lastIndexOf( "string" ); document.write("lastIndexOf found String :" + index ); document.write("<br />"); var index = str1.lastIndexOf( "one" ); document.write("lastIndexOf found String :" + index ); </script> </body> </html> Output: lastIndexOf found String :29 lastIndexOf found String :15
  • 12. replace() Description: This method finds a match between a regular expression and a string, and replaces the matched substring with a new substring. The replacement string can include the following special replacement patterns: Syntax: string.replace(regexp/substr, newSubStr/function[, flags]);
  • 13. replace() Parameters: – regexp : A RegExp object. The match is replaced by the return value of parameter #2. – substr : A String that is to be replaced by newSubStr. – newSubStr : The String that replaces the substring received from parameter #1. – function : A function to be invoked to create the new substring. – flags : A String containing any combination of the RegExp flags: g - global match, i - ignore case, m - match over multiple lines. This parameter is only used if the first parameter is a string. Return Value: – It simply returns a new changed string.
  • 14. replace() Example: Following example shows how to use the global and ignore case flags which permits replace to replace each occurrence of 'apples' in the string with 'oranges'. <html> <head <title>JavaScript String replace() Method</title> </head> <body> <script type="text/javascript"> var re = /apples/gi; var str = "Apples are round, and apples are juicy."; var newstr = str.replace(re, "oranges"); document.write(newstr ); </script> </body> </html>
  • 15. search() Description: – This method Executes the search for a match between a regular expression and this String object. Syntax: – string.search(regexp); Parameters: – regexp : A regular expression object. If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj) Return Value: – If successful, search returns the index of the regular expression inside the string. Otherwise, it returns -1.
  • 16. search() Example: <html> <head> <title>JavaScript String search() Method</title> </head> <body> <script type="text/javascript"> var re = /apples/gi; var str = "Apples are round, and apples are juicy."; if ( str.search(re) == -1 ){ document.write("Does not contain Apples" ); }else{ document.write("Contains Apples" ); } </script> </body> </html> Output: Contains Apples
  • 17. substr() Description: – This method returns the characters in a string beginning at the specified location through the specified number of characters. Syntax: – string.substr(start[, length]); Parameters: – start : Location at which to begin extracting characters (an integer between 0 and one less than the length of the string). – length : The number of characters to extract. Note: If start is negative, substr uses it as a character index from the end of the string. Return Value: – The substr method returns the new sub string based on given parameters.
  • 18. substr() Example: <html> <head><title>JavaScript String substr() Method</title></head><body> <script type="text/javascript"> var str = "Apples are round, and apples are juicy."; document.write("(1,2): " + str.substr(1,2)); document.write("<br />(-2,2): " + str.substr(-2,2)); document.write("<br />(1): " + str.substr(1)); document.write("<br />(-20, 2): " + str.substr(-20,2)); document.write("<br />(20, 2): " str.substr(20,2)); </script></body></html> Output: (1,2): pp (-2,2): Ap (1): pples are round, and apples are juicy. (-20, 2): Ap (20, 2): d
  • 19. substring() Description: – This method returns a subset of a String object. Syntax: – string.substring(indexA, [indexB]) Parameters: – indexA : An integer between 0 and one less than the length of the string. – indexB : (optional) An integer between 0 and the length of the string. Return Value: – The substring method returns the new sub string based on given parameters
  • 20. substring() Example: <html> <head><title>JavaScript String substring() Method</title></head><body> <script type=”text/javascript”> var str = “Apples are round, and apples are juicy.”; document.write( “(1,2): “ + str.substring(1,2)); document.write( “<br />(0,10):” + str.substring(0, 10)); document.write(“<br />(5): “ + str.substring(5)); </script></body> </html> Output: (1,2): p(0,10): Apples are (5): s are round, and apples are juicy.
  • 21. toLowerCase() Description: – This method returns the calling string value converted to lowercase. Syntax: – string.toLocaleLowerCase( ) Return Value: – Returns the calling string value converted to lowercase.
  • 22. toLowerCase() Example: <html> <head> <title>JavaScript String toLowerCase() Method </title></head><body> <script type="text/javascript"> var str = "Apples are round, and Apples are Juicy."; document.write(str.toLowerCase( )); </script></body> </html> Output: apples are round, and apples are juicy.
  • 23. toUpperCase() Description: – This method returns the calling string value converted to uppercase. Syntax: – string.toUpperCase( ) Return Value: – Returns a string representing the specified object.
  • 24. toUpperCase() Example: <html> <head> <title>JavaScript String toUpperCase() Method</title> </head> <body> <script type="text/javascript"> var str = "Apples are round, and Apples are Juicy."; document.write(str.toUpperCase( )); </script> </body> </html> Output: APPLES ARE ROUND, AND APPLES ARE JUICY.