Js--String、Date、Array对象
/*
* String 对象
属性 length
方法
*/
//String的length属性
var strL = "abcde";
document.write("<br/>");
document.write("属性:" + strL.length);
//String的方法
document.write("<br/>");
document.write("方法:" + strL.bold());
document.write("<br/>");
document.write(strL.fontcolor("red"));
document.write("<br/>");
document.write(strL.fontsize(7));
document.write("<br/>");
// sub sup下标
document.write("<br/>");
var str1 = "hello";
var str2 = "world";
document.write("字符串链接:" + str1.concat(str2));
document.write("<br/>");
document.write("返回指定位置字符串,位置没有值返回null:" + str2.charAt(0));
document.write("<br/>");
document.write("返回指定位置字符串的索引,值没有返回-1:" + str2.indexOf("w"));
document.write("<br/>");
var str3 = "a-b-c-d";
var arr1 = str3.split("-");
document.write("split方法切割,切分字符串,成数组:" + arr1.length);
document.write("<br/>");
var str4 = "abcd";
document.write("replace替换,把a替换成e" + str4.replace("a", "e"));
document.write("<br/>");
document.write("substr," + str4.substr(1, 2));//bc 从第1位开始向后截取2位
document.write("<br/>");
document.write("substring:" + str4.substring(1, 2));//b 从第几位开始到第几位结束,不包含最后的那位[1,2)
//数组的3种创建方式
var arr21 = [ 1, 2, 3 ];
var arr22 = new Array(3);
var arr23 = new Array(1, 2, 3);
/**js的Array对象
concat()
join()
push()
pop()
reverse()
*/
var arr11 = [ 1, 2, 3 ];
document.write("<hr/>");
document.write(arr11.length);
document.write("<br/>");
document.write("concat():数组的链接" + arr11.concat());
document.write("<br/>");
var arr12 = new Array(3);
arr12[0] = "a";
arr12[1] = "b";
arr12[2] = "c";
document.write("join():使用指定字符分割数组:" + arr12.join("-"));
document.write("<br/>");
document.write("push():在数组末尾添加一个元素,并且返回新的数组长度:" + arr12.push("d"));
document.write("<br/>");
var arr13 = [ "aaa", "bbb", "ccc" ];
var arr14 = [ "www", "qqq" ];
document.write("在一个数组里用push方法添加一个数组:" + arr13.push(arr14));
document.write("<br/>");
//使用push给数组添加一个数组,他会把这个数组当成元素整体,作为字符串添加到数组中
for (var i = 0; i < arr13.length; i++) {
document.write(arr13[i]);
document.write("<br/>");
}
document.write("<br/>");
var arr14 = [ "zhangsan", "lisi", "wangwu", "zhaoliu" ];
document.write("old length:" + arr14.length);
document.write("<br/>");
document.write("pop()删除最后一个元素,并且返回最后一个元素:" + arr14.pop());
document.write("<br/>");
document.write("new length:" + arr14.length);
document.write("<br/>");
document.write("new arr:" + arr14);
document.write("<br/>");
document.write("reverse():颠倒数组中的顺序:" + arr14.reverse());
/**Date对象
toLocaleString:把日期转换成习惯的日期格式
getFullYear:得到当前年四位
getMonth:得到当前月,返回值是0-11月,得到准值+1
getDay:得到当前星期,返回值是0-6,星期日是0表示
getDate:得到当前的天,1-31
getHours:获得当前的小时
getMinutes():得到当前的分钟
getSeconds():得到当前的秒
getTime():得到毫秒数:返回的是1970-1-1 至今的毫秒数 应用场景:使用毫秒数处理缓存的效果(没有缓存) 例:www.baidu.com?毫秒数
*/
var date = new Date();
document.write("<hr/>");
document.write("本地日期格式:" + date);//获取当前的时间
document.write("<br/>");
document.write("转成习惯的日期格式:" + date.toLocaleString());
document.write("<br/>");
document.write("获取当前的年的方法:getFullYear():" + date.getFullYear());
document.write("<br/>");
document.write("获取当前的月的方法:getMonth():" + (date.getMonth() + 1));
document.write("<br/>");
document.write("获得当前的星期:getDay():" + date.getDay());
document.write("<br/>");
document.write("获得当前的日:getDate():" + date.getDate());
document.write("<br/>");
document.write("获得当前的小时:getHours():" + date.getHours());
document.write("<br/>");
document.write("获得当前的分钟:getMinutes():" + date.getMinutes());
document.write("<br/>");
document.write("获得当前的秒:getSeconds():" + date.getSeconds());
document.write("<br/>");
document.write("获得毫秒数:getTime()" + date.getTime());//返回的是1970-1-1 至今的毫秒数 应用场景:使用毫秒数处理缓存的效果(没有缓存) 例:www.baidu.com?毫秒数
/**Math对象里全是静态方法,使用Math.方法名();
ceil():向上舍入
floor():向下舍入
round():四舍五入
random():随机数
max(x,y):最大值
min(x,y):最小值
pow(x,y):x的y次幂、
*/
document.write("<hr/>");
var mm=10.4;
document.write("ceil():向上舍入:"+Math.ceil(mm));
document.write("<br/>");
document.write("floor():向下舍入:"+Math.floor(mm));
document.write("<br/>");
document.write("round()四舍五入"+Math.round(mm));
document.write("<br/>");
document.write("random()随机数"+Math.floor(Math.random()*10));//获得0-9的随机数
document.write("<br/>");
document.write("pow(x,y):x的y次幂:"+Math.pow(2,5));
Js--String、Date、Array对象的更多相关文章
- javascript - 内置对象 String/Date/Array/Math
1.构建对象的方法 <script> //构建对象方法 //第1种 var people = new Object(); people.name = "iwen"; p ...
- JavaScript---网络编程(3)-Object、String、Array对象和prototype属性
本节学习JavaScript的对象和方法(函数)~ Object 对象 提供所有 JScript 对象通用的功能. obj = new Object([value]) 参数 obj 必选项.要赋值为 ...
- JS基础语法---Array对象的方法
Array对象的方法 Array.isArray(对象)---->判断这个对象是不是数组 instanceof关键字 判断对象是不是数组类型:两种方法: //1 instanceof var ...
- js基础复习~Array对象
Array对象 lenght 获取到数组的长度 concat() 方法用于合并两个或多个数组.此方法不会更改两大有数组,而是返回一个新的数组 let arr1 = ["a",&qu ...
- JS中部分 Array 对象方法介绍
1.concat() concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本 <script type="text/javascrip ...
- js中数组Array对象的方法sort()的应用
一. sort()方法的介绍 //给一组数据排序 var arrNum = [12,1,9,23,56,100,88,66]; console.log("排序前的数组:"+arrN ...
- JS内置对象-String对象、Date日期对象、Array数组对象、Math对象
一.JavaScript中的所有事物都是对象:字符串.数组.数值.函数... 1.每个对象带有属性和方法 JavaScript允许自定义对象 2.自定义对象 a.定义并创建对象实例 b.使用函数来定义 ...
- JavaScript (六) js的基本语法 - - - Math 及 Date对象、String对象、Array对象
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Math 1.Math对象的案例 var result= Math.max(10,20,30,40) ...
- 内置对象(Math、Date、String、Array、基本包装类型)
一.内置对象 js中三种对象:内置对象.自定义对象.浏览器对象 实例对象是指通过构造函数创建出来,然后实例化的对象(new关键字) 静态对象是指不需要创建,直接调用的对象,可以在整个JS里调用的公共对 ...
- JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)
String对象(*) length属性:获取字符串的字符个数.(无论中文字符还是英文字符都算1个字符.) charAt(index)方法:获取指定索引位置的字符.(索引从0开始) indexOf(‘ ...
随机推荐
- linux命令之df
功能:查看文件系统的磁盘空间使用情况 常用选项: -a 包含虚拟文件系统 -h 可易读单位显示 -i 显示 inode 信息而非块使用量 -k 1K 块的数量 -t 只显示指定文件系统为指定类型的信息 ...
- 论文笔记---Deblurring Shaken and Partially Saturated Images
抖动和部分饱和图像去模糊 摘要 我们解决了由相机抖动造成的模糊和饱和或过度曝光像素导致的图像去模糊的问题.饱和像素对于现有的非盲去模糊算法是一个问题,因为它们不符合图像形成过程是线性的这一假设,并且经 ...
- express+vue+mongodb+session 实现注册登录
上个月写了一篇文章是 express+mongodb+vue 实现增删改查. 只是简单的实现了增删改查功能,那么今天是在那个基础之上做了扩展,首先实现的功能有如下: 1. 支持注册,登录功能,用户可以 ...
- 工具 Windows安装Anaconda
下载 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 安装 1.勾选添加Anaconda到PATH环境变量 2.配置清华镜像 conda ...
- Mac环境 安装brew
一.brew官网主页上的方法: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/insta ...
- redis底层设计(二)——内存映射数据结构
我们继续接着上一篇博客,今天来看看内存映射数据结构. 上篇我们讲了内部数据结构,虽然内部数据结构非常强大,但是创建一系列完整的数据结构本身也是一件相当耗费时间的工作,当一个对象包含的元素数量并不多,或 ...
- 构建前端gulp自动化
看了很多关于Gulp自动化的相关教程,很感谢大神们的教程, 因为担心自己会忘记啥的,所以就把自己搭建gulp自动化的过程记录下来~~~ gulp是依赖于Nodejs的,所以最好是有点nodejs的基础 ...
- 牛客网 Python 编程输入规范
import sys try: while True: line = sys.stdin.readline().strip() if line == '': break lines = line.sp ...
- hdu 1730 Nim博弈
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1730 Nim博弈为:n堆石子,每个人可以在任意一堆中取任意数量的石子 n个数异或值为0就后手赢,否则先 ...
- MySQL 通过多个示例学习索引
最近在准备面试,关于索引这一块,发现很多以前忽略的点,这里好好整理一下 首先为什么要建立索引 一本书,有章.节.段.行这种单位. 如果现在需要找一个内容:第9章>第2节>第3段>第4 ...