字符练习

<script type="text/javascript">


/* indexOf() 用于查找字符串有是否有这个字符,返回索引
var the_email = prompt("what\'s your email address?","");
var temp = the_email.indexOf('@');
if(temp==-1){
alert("your email have not @ char.");
}
*/

/* charAt() 可以返回字串中的某个字符
var the_word = "monkey";
var the_last_word = the_word.charAt(the_word.length-1);
alert(the_last_word); //output y
*/

/*

substring() 可以截取一个字串中的几个字符,而不是单个。语法 : substring(form,to) length = to -form, to 的长度是整个字符长度
var the_string = "monkey";
var cut_it = the_string.substring(3,4);
alert(cut_it); //output k
*/


/*
var the_string = "http://www.webmonkey.com/javascript/index.html";
var first_slashes = the_string.indexOf("//");
// alert(first_slashes); output 5
var start_domain = first_slashes+2;

var without_resource = the_string.substring(start_domain,the_string.length);
// alert(without_resource); output www.webmonkey.com/javascript/index.html
var sec_slash = without_resource.indexOf("/");
var domain = without_resource.substring(0,sec_slash);
alert(domain); //output www.webmonkey.com
*/

/* split() 将字符转换成数组
var my_friends ="trixie,moxie,sven,guido,hermes";
var friend_array = my_friends.split(",");

for(var i=0;i<friend_array.length;i++){
document.write(friend_array[i]+"<br/>");
}
*/

/*
var the_string = "http://www.webmonkey.com/javascript/index.html";
var first_split = the_string.split("//");
var without_resource = first_split[1];
var sec_split = without_resource.split("/");
var domain = sec_split[0];
alert(domain);
*/

</script>
posted @ 2011-04-22 16:06  e.e.p  阅读(163)  评论(0编辑  收藏  举报