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
Category:Basics
Date:2008-02-05
Level:Beginner
Product:See complete product
Viewed:69284

Bookmark JavaScript string tutorial



AddThis Social Bookmark Button

Step 3 - More string manipulation functions


JavaScript string tutorial

 

indexOf

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. 

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

split

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.

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

substr 

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.

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

substring

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.

Code:
  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


F1 Site Family
AJAX F1
CSS F1
Database F1
Flash F1
HTML F1
Java F1
JavaScript F1
PhotoShop F1
PHP F1
Scripts F1
Tutorial F1
Windows F1

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

Total time: 0.1836