JavaScript string tutorial - More string manipulation functions

Home - Tutorials - Basics

In this tutorial you will learn how to use and manipulate strings in JavaScript.

Tutorial info:

Name:JavaScript string tutorial
Total steps:3
Product:See complete product

Bookmark JavaScript string tutorial

AddThis Social Bookmark Button

Step 3 - More string manipulation functions

JavaScript string tutorial



Synatx: object.indexOf(searchValue[,index]); 

You can use indexOf() method to check whether a string or character is present in your main string. You can use it only with a string parameter. In this case the indexOf method returns the position where the string was found inside your main string. If there is no such sub-string then it returns with -1.

Besides this you can also use the function with 2 parameters. In this case the first is the string as before and the second is a number which tells the method from where to start the search. 

  1. var str = 'Demo Text.';
  2. var pos = str.indexOf('Te');
  3. var pos2 = str.indexOf('Te',10);


Syntax: object.split(separator); 

With split you can divide a string into more parts and the separated parts will be loaded into an array. For example you can divide a sentence into words by using split and a space character as separator.

  1. var str = 'Demo Text.';
  2. var words = new Array();
  4. words = str.split(' ');


Syntax: object.substr(start[,length]); 

With substr() method you can cut one piece of the original string. The method requires 2 parameters. The first defines where you want to start the cut and the second shows how many character do you want to cut.

  1. var str = 'Demo Text.';
  2. var str1 = str.substr(3,4);


Syntax: object.substring(start,end); 

Similar to substr but in this case the second parameter defines not the length but the last position of the cut.

  1. var str = 'Demo Text.';
  2. var str1 = str.substring(3,4);

Previous Step of JavaScript string tutorial

Tags: javascript string functions, javascript string, javascript, string, function

JavaScript string tutorial - Table of contents
Step 1 - JavaScript string basics
Step 2 - String manipulation with JavaScript
Step 3 - More string manipulation functions

GAD Tower

Family tutorials
PHP Array
PHP Redirect
PHP Session
AJAX File Upload
AJAX Rating System
MySQL Create User
Mysqldump Example
MySQL Concat

Total time: 2.899