Use of length, charAt() and IndexOf() string function function in javascript with example
length
The length method returns the length of a particular string as a number. For instance, if a string is 5 characters long, the length method would return the number 5. To use the method, you will want your string to be placed in a variable. For this example, we will simply assign a string to a variable:
var my_name="Rashmi";
var how_long= my_name.length;
Eample
<SCRIPT
language="JavaScript">
<!--
function getname()
{
var yourname=prompt('Enter your name, NOW!','');
if (yourname.length > 10)
{
alert('That name is just too long, give me a shorter one!');
getname();
}
else
alert('Hi '+yourname+'!');
}
//-->
</SCRIPT>
charAt
Using the charAt method, you can find out what character is filling a designated position within a string. Basically, it allows you to find out what character is first, second, third, and so on. The important thing to remember is that when dealing with the position of a character, the count starts at zero, not one. So the first character is actually at position zero.
var my_name="Rashmi";
var the_char= my_name.charAt(0);
alert('The 1st character is '+the_char+'.');
The output will be ‘R’
indexOf
Now we get to the really fun method of indexOf. This method lets you check for a character or even a small string within the current string, and returns the position at which your character or string begins. Again, remember that the position starts counting at 0, so if indexOf returns zero, it means your character or string begins at the 1st character. Also, indexOf has a useful return value of -1 if the character or string you searched for is not contained within the string. And finally, if the character or string you search for occurs more than once, indexOf returns only the first appearance of your character or string.
First, let's find the 'a'. No problem, again we use the dot operator. IndexOf also takes an argument like charAt does, but this time the argument is the character or string we are looking for:
var my_name="Rashmi";
var where_is_a= my_name.indexOf('a');
alert('The a is at position '+where_is_a+'.');
Notice the quote marks around the argument of a. This tells the browser to look for that string, which is the type of argument you must send for indexOf to function properly. This code will send back the position of the letter a. The alert will say "The a is at position 1." Look out letter a, you have been found!
What about if we search for something that is not there? Well, indexOf will return a -1. We can use this -1 to tell us that what we are looking for is not there. This can be helpful when you ask a viewer for information, but the viewer does not type the information with a required character or set of characters. For instance:
Check for URL or Validate URL in javascript
function get_url()
{
var your_url=prompt('Enter your web page URL!','');
var is_protocol_ok=your_url.indexOf('http://');
if (is_protocol_ok==-1)
{
alert('Error: Your url should begin with http://');
get_url();
}
else
alert('Thanks!');
}
This one checks to be sure the viewer entered the http:// part of the address. If not, it shows the error alert and gives the viewer a chance to enter the url again. Of course, you can cut down on the number of forgotten http:// beginnings by using http:// as the default value for the prompt instead of nothing:
var your_url=prompt('Enter your web page URL!','http://');
Of course, the viewer could delete it by accident (or even on purpose), so the check would still be worthwhile. You could even go a step further and be sure that there is at least one dot (.) in the address as well. This way, the viewer would need to have a string plus a dot and hopefully a com or net afterward ( like http://oops-solution.blogspot.com/). We can also check for the http:// as well, here is a sample:
function get_url()
{
var your_url=prompt('Enter your web page URL!','http://');
var is_protocol_ok=your_url.indexOf('http://');
var is_dot_ok=your_url.indexOf('.');
if ((is_protocol_ok==-1) || (is_dot_ok==-1))
{
alert('Error: Your url should begin with http:// and have at least one dot (.)!');
get_url();
}
else
alert('Thanks!');
}
good
ReplyDelete