JavaScript string tutorial - String manipulation with JavaScript


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

Bookmark JavaScript string tutorial



AddThis Social Bookmark Button

Step 2 - String manipulation with JavaScript


JavaScript string tutorial

The most important string manipulation functions are the following:

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:
  1. var str = 'Demo text.';
  2. var len = str.length;
  3. 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:
  1. var str = 'Demo Text.';
  2. var strL = str.toLowerCase();
  3. var strU = str.toUpperCase();
  4.  
  5. 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:
  1. var str = 'Demo Text.';
  2. var c = str.charAt(5);
  3.  
  4. for (i=0;i<str.length;i++){
  5. alert(str.charAt(i));
  6. }




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



GAD Tower


F1 Site Family
AJAX F1
CSS F1
Database F1
HTML F1
Java F1
JavaScript F1
PHP 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.1493