JavaScript string tutorial - More string manipulation functions
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: | 65213 |
Bookmark JavaScript string tutorial
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:
var str = 'Demo Text.'; var pos = str.indexOf('Te'); 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:
var str = 'Demo Text.'; var words = new Array(); 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:
var str = 'Demo Text.'; 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:
var str = 'Demo Text.'; 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 |