SlideShare ist ein Scribd-Unternehmen logo
1 von 25
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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Java String
Java StringJava String
Java String
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
 
Javascript
JavascriptJavascript
Javascript
 
Css
CssCss
Css
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
String in java
String in javaString in java
String in java
 
Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
Django
DjangoDjango
Django
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 
Java script
Java scriptJava script
Java script
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Php string function
Php string function Php string function
Php string function
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
Java script
Java scriptJava script
Java script
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
css.ppt
css.pptcss.ppt
css.ppt
 
CSS
CSSCSS
CSS
 

Ähnlich wie Javascript built in String Functions

JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript ObjectsReem Alattas
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Dhivyaa C.R
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeDhivyaa C.R
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objectsPhúc Đỗ
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text ProcessingIntro C# Book
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notesGOKULKANNANMMECLECTC
 

Ähnlich wie Javascript built in String Functions (20)

Javascripting.pptx
Javascripting.pptxJavascripting.pptx
Javascripting.pptx
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
Java script arrays
Java script arraysJava script arrays
Java script arrays
 
Java script arrays
Java script arraysJava script arrays
Java script arrays
 
Strings
StringsStrings
Strings
 
WD programs descriptions.docx
WD programs descriptions.docxWD programs descriptions.docx
WD programs descriptions.docx
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
 
UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
UNIT II (7).pptx
UNIT II (7).pptxUNIT II (7).pptx
UNIT II (7).pptx
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering CollegeString handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
Unitii string
Unitii stringUnitii string
Unitii string
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 

Kürzlich hochgeladen

ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Kürzlich hochgeladen (20)

ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 

Javascript built in String Functions

  • 1.
  • 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.