-
原文地址http://www.2cto.com/kf/201112/115476.html
-
-
-
js中数组和字符串入门
var arr1=new Array(4); //定义数组
arr1[0]=["321"];
arr1[1]=["211"];
arr1[2]=["1111"];
var arr2=["111","2222","33333"];
arr2.push("4444","55555"); //添加一个数组(在后面)----栈
arr1.pop();//删除最后一个元素
arr1.shift();//删除第一个元素
arr1.unshift("aaaaaa","bbbbbb");//插入第一个元素
arr1.push("4332"); //
//slince\截取字符串 sort(function(a,b){return a-b;}) a-b大于0,a排在b后面、prompt("","")
//String slice(开始字符串,结束字符串)、subString(str,end)、substr(start,length),后两个返回0,-1,1 slince--》返回字符串
document.write(arr1.join("*")+"</br>"); //输出所有元素,用*分割
document.write(arr1.toString()+"</br>"); //输出所有元素,用,分割
document.write(arr1.reverse()+"</br>"); //反转数组
document.write(arr1.sort()+"</br>"); //排序
document.write(arr1.toString().charAt(2)+"</br>"); //返回指定位置的字符,charCodeAt
document.write("indexOf:"+arr1.toString().indexOf("a",1)+"</br>");
var gxf="hello,beijng welcome to you !!";
document.write("gxf"+gxf.slice(3,8)+"</br>");
document.write("gxf"+gxf.substring(4,5)+"</br>");
document.write("gxf"+gxf.substr(5,4)+"</br>");
document.write("gxf".localeCompare(gxf)+"</br>");
arr1.splice(3,2,"gggg","fffff","xxxxxxx","gggggggg"); //从头开始删除2个元素,
var arr3=arr1.concat(arr2);
for (var i=0;i<arr1.length-1;i++){
document.write("第"+i+"个数组为:"+arr1[i]+"</br>");
}
for(var i in arr2){
document.write("---"+arr2[i]+"<br>");
}
for(var i in arr3){
document.write("conCat-arr3:"+arr3[i]+"<br>");
}
/* //Js模拟 二维数组 www.2cto.com
var arr3=new Array(['12343','12231','123131'],['asdcw'],['bfddssa'],['dddddddddd']);
// var arr3 = new Array(['a','b','c'],['d','e','f']);
// var arr3[]=[["123","222","2332"],["aaa","bbbb","cccc"]];
for (var h=0;h<2;h++){
for (var j=0;j<3;j++){
document.write("arr3:"+arr3[h][j]+"</br>");
}
}
*/
</script>