Page 170 - DCAP408_WEB_PROGRAMMING
P. 170
Web Programming
Notes 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.
Example: 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.
Example: See the examples:
Code:
var str = ‘Demo Text.’;
var c = str.charAt(5);
for (i=0;i<str.length;i++){
alert(str.charAt(i));
}
164 LOVELY PROFESSIONAL UNIVERSITY