JavaScript string tutorial - String manipulation with JavaScript
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: | 44916 |
Bookmark JavaScript string tutorial
Step 2 - String manipulation with JavaScript
JavaScript string tutorial
The most important string manipulation functions are the following:
- length
- toLowerCase
- toUpperCase
- charAt
- indexOf
- split
- substr
- subString
Now let's see one by one what can we do with them and how to use them in a JavaScript code.
length
Syntax: object.length;
This function returns with the total number of characters in the string, or more simple it returns with the length of the string. The usage is quite simple:
Code:
var str = 'Demo text.'; var len = str.length; var len2 = 'my other text'.length;
toLowerCase and toUpperCase
Synatx: object.toLowerCase();
These methods can be very useful if you want to all character in a string lower case or upper case. For example if you want to compare 2 strings but you are not sure if they are capitalized or not. In this case just convert both of them to lower or upper case and you can make the comparison without any problem.
Usage example:
Code:
var str = 'Demo Text.'; var strL = str.toLowerCase(); var strU = str.toUpperCase(); if (str.toLowerCase() == 'demo text.')
charAt
Synatx: object.charAt(index);
With this function you can get the character at the given position in a string. If you want you can read the complete string character by character using charAt and a loop. See the examples:
Code:
var str = 'Demo Text.'; var c = str.charAt(5); for (i=0;i<str.length;i++){ alert(str.charAt(i)); }
Previous Step of JavaScript string tutorialNext 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 |