JavaScript Strings

String Object

String objects are used to store and manipulate text data. It is primitive data type in JavaScript with number of properties and helper methods to manipulate data. The string object is created as follows:

var str=new String(stringValue);

In JavaScript you don’t need to create string object like above syntax because the numeric literals are treated as instances of String class. A string is text inside quotes. You can use single or double quotes as below.

var str=”ABC”;
var str=’ABC’;


String Properties

Property Description
constructor Returns the reference to the function that creates string
length Returns the length of the string
prototype Allows to add methods and properties to an object

Ex: var str= ’C# Corner’;
var len=str.length; // len=9


String Methods

The following are the string methods.

  1. charAt():

    This method returns the character at specified index in string. It takes one argument ‘Index’ and returns character at that index. Index starts from 0.

    Syntax: charAt(index);
    Ex. var str= ‘India’;
    var result=str.charAt(2); // result = d


  2. charCodeAt ():

    This method returns the number indicating the Unicode value of the character at the given index.

    Syntax: charCodeAt(index);
    Ex. var str= ‘India’;
    var result=str.charCodeAt(4); // result = 65

  3. concat():

    This method combines text of two or more strings and returns a new string. It takes number of strings as arguments and combines them.

    Syntax: concat(string1, string2, …);
    Ex. var str= ‘India’;
    var str1= ‘ is’;
    var str2= ‘ best’;
    var result=str.concat(str1,str2); // result = India is best

  4. indexOf():

    This method returns the index of the first occurrence of the specified value within calling string. If the specified value is not found then it returns -1. It takes two parameters, value to search and starting index for search. The second on start index is optional parameter. If it is not specified then search start from index 0.

    Syntax: indexOf(searchValue [,startIndex]);
    Ex. var str= ‘India is best’;
    var result=str.indexOf(‘is’); // result = 6
    var result=str.indexOf(‘isa’); // result = -1
    var result=str.indexOf(‘s’,8); // result = 8

  5. lastIndexOf():

    This method returns the index of the last occurrence of the specified value within calling string. If the specified value is not found then it returns -1. It takes two parameters, value to search and starting index for search. The second on start index is optional parameter. If it is not specified then search start from index 0.

    Syntax: lastIndexOf(searchValue [,startIndex]);
    Ex. var str= ‘India is best’;
    var result=str. lastIndexOf (‘s’); // result = 11

  6. match():

    This method matches regular expression against a string. It returns array containing matched results or null if there were no matches. It takes one parameter that represents the regular expression to be matched.

    Syntax: match(regexp);
    Ex.
    var str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    var regexp= /[A-E]/gi;
    var result=str.match(regexp); // result = A,B,C,D,E,a,b,c,d,e

  7. replace():

    The replace() method replaces a specified value with another value in string. This method does not change original string instead it returns new string. This method can also take regular expression as the search value for replacement. By default replace method replace only first match. To replace all matches, use regular expression with ‘g’ flag.

    Syntax: replace(substr|regexp, newstr);
    Ex.
    var str= 'Apples are round, and apples are juicy.';
    var result= str.replace('Apples', 'oranges');
    // result= oranges are round, and apples are juicy.
    var regexp= /apples/gi;
    var result1=str. replace (regexp,’ 'oranges'’);
    // result1 = oranges are round, and oranges are juicy.

  8. search ():

    This method executes the search for match between regular expression and the string. It returns the position of the first match in string. If specified value not found then it returns -1.

    Syntax: search(regexp);
    Ex.
    var str= 'Apples are round, and apples are juicy.';
    var result=str.search(‘Apples’); // result = 0

  9. slice():

    This method extracts the part of the string and return extracted part in new string. It takes two parameters start index and end index. The parameter end index is optional. If end index is not provided then slice() extracts to the end of string.

    Syntax: slice(startIndex [,endIndex]);
    Ex.
    var str= 'Apples are round, and apples are juicy.';
    var result=str.slice(7); // result = are round, and apples are juicy.
    var result1=str.slice(0,6); // result1 = Apples

  10. substr():

    The substr() method returns the characters in a string beginning at the specified location through the specified number of characters. It takes two parameters start and length. Length is optional parameter. If start is negative then substr() uses it as a character index from the end of the string.

    Syntax: substr(start[,length]);
    Ex.
    var str= 'Apples are round, and apples are juicy.';
    var result=str.substr(0,3); // result = App
    var result1=str.substr(3); // result1 = les are round, and apples are juicy.

  11. substring():

    The substring () method returns subset of string between given two indexes. It takes two parameter start index and end index. The end index parameter is optional. The difference between slice () and substring () method is that substring () method does not take negative parameters.

    Syntax: substring(startIndex[,endIndex]);

    Ex.
    var str= ‘Apple, Banana, Orange’;
    var result=str.substring(15); // result = Orange
    var result1=str. substring (7,13); // result1 = Banana

  12. toLowerCase():

    This method returns the calling string value converted to lowercase.

    Syntax: toLowerCase()
    Ex.
    var str= ‘INDIA’;
    var result=str.toLowerCase(); // result = india

  13. toUpperCase():

    This method returns the calling string value converted to uppercase.

    Syntax: toUpperCase()
    Ex.
    var str= ‘india;
    var result=str.toUpperCase(); // result = INDIA

  14. valueOf():

    This method returns the primitive value of string.

    Syntax: valueOf()
    Ex.
    var str= ‘india;
    var result=str.valueOf(); // result = india

Up Next
    Ebook Download
    View all
    Learn
    View all