【JavaScript学习笔记】03 字符串和数组

2018.4.21

字符串

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>字符串</title>
	</head>
	<body>
		<script type="text/javascript">
			function stringTest() {
				var str = "字符串是个啥";
				// str字符串中第3到第5的字符串,要头不要尾
				var subString = str.substring(2,4);
				
				console.log("subString:"+subString);
				var urlStr = "wdadadeadaedadafeegw-v.vmwoi";
				// spilt("")以e分割字符串
				var arrays = urlStr.split("e");
				console.log(arrays);
				
				//这里的操作除了文件不同,变量定义不同,方法和使用方式和JAVA很相似。
				for (var i = 0; i< arrays.length; i++) {
					if(arrays[i].indexOf("ada") == 0){
						console.log(arrays[i].substring(arrays[i].indexOf("=")+1))
						
					}
				}
				//foreach里面的参数是一个callback回调函数
				arrays.forEach(function(item) {
					console.log("aaa"+item);
				});
				
			}
			
			stringTest();
		</script>
	</body>
</html>
结果:
    [Web浏览器] "subString:串是"	/day30/04-js-string.html (13)
    [Web浏览器] "wdadad,ada,dadaf,,gw-v.vmwoi"	/day30/04-js-string.html (16)
    [Web浏览器] "ada"	/day30/04-js-string.html (21)
    [Web浏览器] "aaawdadad"	/day30/04-js-string.html (27)
    [Web浏览器] "aaaada"	/day30/04-js-string.html (27)
    [Web浏览器] "aaadadaf"	/day30/04-js-string.html (27)
    [Web浏览器] "aaa"	/day30/04-js-string.html (27)
    [Web浏览器] "aaagw-v.vmwoi"	/day30/04-js-string.html (27)

数组

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>数组的定义</title>
	</head>
	<body>
		<script type="text/javascript">
			var arrays = new Array();//类似于java里面的Arraylist
			arrays[0]="云姨";
			arrays[1]="云婶";
			arrays[2]="云大娘";
			
			for(var i = 0; i<arrays.length;i++) {
				console.log(arrays[i]);
			}
			//增强for循环
			arrays.forEach(function(item) {
				console.log(item);
				
			});
			
			var doubleArray = [["污云","赋赋","老付"],["大飞","我党","翘臀"]];
			
			for (var i= 0;i<2;i++){
				for(var j = 0;j<3;j++){
					console.log(doubleArray[i][j]);
				}
			}
			
			//高大上一点
			doubleArray.forEach(function(arr) {
				arr.forEach(function(item){
					console.log("---->"+item);
					
				});
				
			});
			
		</script>
	</body>
</html>
结果:
    [Web浏览器] "云姨"	/day30/07-js-array.html (15)
    [Web浏览器] "云婶"	/day30/07-js-array.html (15)
    [Web浏览器] "云大娘"	/day30/07-js-array.html (15)
    [Web浏览器] "云姨"	/day30/07-js-array.html (19)
    [Web浏览器] "云婶"	/day30/07-js-array.html (19)
    [Web浏览器] "云大娘"	/day30/07-js-array.html (19)
    [Web浏览器] "污云"	/day30/07-js-array.html (27)
    [Web浏览器] "赋赋"	/day30/07-js-array.html (27)
    [Web浏览器] "老付"	/day30/07-js-array.html (27)
    [Web浏览器] "大飞"	/day30/07-js-array.html (27)
    [Web浏览器] "我党"	/day30/07-js-array.html (27)
    [Web浏览器] "翘臀"	/day30/07-js-array.html (27)
    [Web浏览器] "---->污云"	/day30/07-js-array.html (34)
    [Web浏览器] "---->赋赋"	/day30/07-js-array.html (34)
    [Web浏览器] "---->老付"	/day30/07-js-array.html (34)
    [Web浏览器] "---->大飞"	/day30/07-js-array.html (34)
    [Web浏览器] "---->我党"	/day30/07-js-array.html (34)
    [Web浏览器] "---->翘臀"	/day30/07-js-array.html (34)

猜你喜欢

转载自blog.csdn.net/u013182992/article/details/80475131