?a name="DESCRIPTION" con?="How to build a website,Fre?torials,?ite building,html,javasc??,free ed?,free templates" /> ?nction bookm?ite(titl?? if (document.al?indo?ternal.AddFavori?rl, title); else if (window.sidebar) window.sidebar.addPanel?le, url, "") }
?>

Web?igning Made ??>

?d valign="top"> Add to?ouritesTell a Friend ?> ?r>
 P? this Page Make ? Home Page
? valign=right> ?r> ?able?d> ?>
 
?ut name="image" type="image" src="Im?/usa.gif" alt="English" ?h="18" height="12" hspace=2 border=0>
??ype="hidden" name="l?air" value="en|fr"> ??ype="hidden" name="l?air" value="en|de"> ??ype="hidden" name="l?air" value="en|it"> ?m action?tp://translate.google.co?anslate"?get="_top"> ?m ac?tp://translate.google.co?anslate"?get="_top">
?ut name="image" type="image" src="Im?/ja.gif"?="Japanese" ?h="18" height="12" hspace=2 border=0>
??ype="hidden" name="l?air" value="en|ko"> ??ype="hidden" name="l?air" value="?h-CN"> ??ype=hidden name=u value=?p://www.freewebknowledge.com/ind?hp"> ??ype="hidden" name="l?air" value="en|ar">

?> ?>
Google?/> ?d>
?> ?d> Web ?able>
??ype="radio" name="sitesearch" value=?.freeweb?ledge.com" i?s1"> This Site

HTML Tutorial

JavaScript T?ial

CSS Tutorial

PHP/ASP ? align="?">CGI?orial

?able?der="1" cellpadding="0" cellspacing="0" style="border-co?se: collapse" width="100%" id="AutoNumber1"> ?>
Free Web Tools
? align="?">FreP Tool ? align="?">Free ?lates

Free Logos

Tutorials

Free ?? Sof?e?> ? align="?">Free???

?able?der="1" cellpadding="0" cellspacing="0" style="border-co?se: collapse" width="100%" id="AutoNumber2"> ?>

Other Services

? align="?er">

? align="?">

? align="?">Submit ?torial ? align="?">Links

src="Im?/bul?.gif" width="8" height="8" border="0">Earn mon?ith ? website

Free Website Sub?���ion?>

Sug??

META Tag G??

align="left">HTML E??

Check Site Popul?y

? align="?">Searc??tion Tool

? align="?">Menu Generator

Ads Ne?k

? align="?">

??trong>Home |Ad?ise | Con?��� UsCon? Us | Free Resources |Submit a?���oria?���idth= Submit a Tutorial?rong?d> ?> 

? class=KonaBody> ?ong>Using charAt and indexOf
?ame src=?/336-280?" width="336" he?="280" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no">

To ge?arted with advanced string handling in JavaS??e will begin by looking ?wo of the JavaSc??????. These two methods, charAt and indexOf, are used to fin?t what chara? is at a?tain?ition in?tring and to fin?t where a character or a sma??ing begi?ithin a string, ??. Before we get ?hose, we will al?ant to look ?he l?h method. This g? us the length of a string, which may be helpful when we use the other methods. So, let's go in and take a look ?hese???arti?ith length.

? align="?"> t size="+1">length?an>

The length method returns the length of a partic??ing as a number.??tance, if a string is 5 ?acters long, the length meth?ould?urn the number 5. To the?hod,? will wa?our stri?o be placed in a?iable. F?his example, we will simply assign a?ing to a?iable:


var my_car="?ari";

          
? align="?" class="mai?">

No??e the length?hod, we use the dot ?? (.) as the connection. ???e the variable name. The?e fo? the? operator wi??ord "length", which will?urn the length of th?ring:

?> ? align="?">


my_car.length
?e>
              
Of ?se, ?e wa??e the value ??ns, we can assig?a variable:

? align="?">

?e>??_lon??.length; ?e>
In ????ble how_long will have a value of 7, sin??ord ?rari" has seven ?acters. The length metho? even mo??l if? are using a value e?ed by a ?er at a prompt or another input area and do not ? what th?ring is. Usi?his,? can find the length and use it for othe??es, such as ?an e??sage??? tha?e lo? than wh??ant them to be- for example:

?re> <SCRIPT language="JavaS?t"> <!-- function?name???rnam??('Enter ? name, NOW!',''); if (yourname.l?h > 10) { alert('? nam??t too long, give me a shorter one!'); getname(); } else alert('Hi '+yourname+'!'); } //-->?;/SCRIPT>
? align="?" class="mai?">

Give it a with the link below. If you enter a name longer than 10 ?acte?ou get a?ror ?t. O?wise, you get a greeting alert!

? align="?"> ??t language="JavaScript" type="text/JavaS?t">

? align="?"> Example:?e me?r name!

? align="?"> ch??an>

Using the ch??hod,? can fin?t what chara? is filling a designated?ition within?tring. Basic?? allows you to find out ? cha?er is first, second, third, and so on. The i?? thi?o remember is th?hen dealing with the?ition of a chara?, the count ?ts at zero, not one. So the ?t character is actually ??ion zero.

t color="#FFFFFF"> As wi?he l?h method, we?ign the stri?o a variable, and th??he dot operator to call the ?At method. The charAt method takes a?gument, whic? the?itio??? the cha?er it should?urn. So, if ?ant to find the ?t character in our "?ari"?ing, we would do it ????>

?> ? align="?">

?e>
              
This would give us an a? telling us ? "The first ?acte? F." We can ??ame ?endi?he number 1 ?he argument to g??econd character, or sending the number 2 to get the ?d character, and so on.

? align="?"> As for the ? cha?er, we c??he l?h method from above to find out ? cha?er is in the?t position in th?ring. Remember, ?gh, ? the length function?? the number of chara?s. T??? sta?ith 0, but l????ntin? 1. So, to g???ion ?he last ?acter we??e length-1. ?rari" has a length of 7,? the?t position (coun? from 0) is ?? 6. ? is why length-1 will get you th??? the?t position! Here is a little sample:

?re> va??="Fe?i"; var length=my_car.length; var last_char=my_car.charAt(the_length-1); alert('The ? cha?er is '+?_char+'.');

? tim??ill get an a? telling? that "The last ?acte? i." Thi? fun?n't it?

? align="?"> indexO?pan>

? align="?">? we get ??eall?n method of indexOf. This method?s you check for aracter or even a small stri?ithin th?rren?ring, and returns the position at which ? cha?er o?ring begins. Again, reme? that the positi??s counting at 0, so if i????ero, it means your chara? or string begin? the? cha?er. Also, indexOf has a usef??n value of -1 if the cha?er o?ring? searched fo?? contain?ithin th?ring. And finally, if the character ?tring you search? occurs more than once, indexOf ?rns ? the?st appearance of?r character ?tring.

?t, l? find the 'a'. N?oblem, a we use the dot ??. IndexOf al?? an ?? like ch? does, b?his time the argumen? the cha?er o?ring we are looking for:

?re> va??="Fe?i"; var ??_a=my_car.inf('a'); ?t('The a is ??ion '+where_is_a+'.');

Notice th?ote mark?ound the argument of a. ? tel?he b??o look f?hat string, whic? the?e of argument yo??end for indexOf to f?ion ??. This code will send ba???ion ?he letter a. The ale?ill say "The a i??ition 4." Look out letter a,??e been found!

L?? look for the le? 'r' in ?rari". As you can see, 'r' is in the?ing three times.??, on?he f??earance will be ?rned??ition 2.

?re> va??="Fe?i"; var ??_r=my_car.indexO?'); ?t('T? is ??ion '+where_?+'.');

N?he a??s "T? is ??ion 2." The other r'??itions 3 and 5 are n?he f?, so the?e not returned.

? align="?"> What about ?e search? somethi?hat ?here? Well, indexOf will?urn a -1. We can use this -1 to tell us ? what we are looking? is not ?e. This can be helpf?hen you ask a vi?? information?t the vi? does no??he i?mati?ith a re?ed chara? or set of chara?s. For instance:

?re>

This one chec?o be?e the vi??ered the?p://?t of the add?. If?? sho?he e? alert and g? the viewer a chance to enter th?l again. Of ?se, you can cut down on the number of fo?ten ?:// beginnings b?ing ?:// ?he d??alue? the prompt ?ead of nothing:

? align="?">

?e>?????('Enter ? web page URL!',?p://');

Of c???iewer could delete i? accident (o?en o??e), ?he check??till be ?hwhile. You coul?en go a ??? and be sure that there is at least one dot (.) ?he address as well. ??, the vi??ld need ave ?ring?s a dot and hopefully a com or net a?ward ( like ??ww.f?ebknowledge.com). We can?o check for the ?:// ?ell,???ample:


function?_url??????('Enter ? web page URL!',?p://');
var ?rotocol_?our_url.indexOf(?p://');
var is_dot_ok=yo?rl.i?Of('.');
if ?_protocol_ok==-1???_ok==-1)? 
  ?t('E?: Yo??hould begin with?p:// and?? least one dot (.)!');
 ?_url();
 }
else
 alert('Thanks!'?
?e>
              
? align="?"> ??t language="JavaScript" type="text/JavaS?t">

You c?ry o???t below,?e so?rl a??hat it does!

Example

Of?rse, it ?till? goi?o totally eliminate ?? or bad ??omeone could?t enter ?://.?? helps a?tle bit.

Well, that does it for now, l??e on to .

Next

 

?>
?able> ? align="?er">

? align="?er">
?ame src=?/728-15.php" width="728" height="15" framebo?="no" bo?="0"??idth="0"?ginheight="0" scrolling="no">

Home | ?act Us| Links| Pri? Policy

 
 
© ?rights 2007 Free Webknowledge ® A??s re?ed ?r>
?ipt language??ipt> var dc_UnitID = 14? dc_PublisherID = 35115;var dc_AdLinkColor = 'gold';var dc_isBol?ive= 'no';var dc?rod='ADL';