Js--String、Date、Array对象

摘要:
/**字符串对象属性长度方法*///String varstrL=“abcde”的长度属性;文件写入(“”);文件write(“Attribute:”+strL.length)//字符串方法文档。写入(“”);文件write(“方法:”+strL.bold());document.write(“˂

/*
* 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对象》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Windows7下 部署 ASP.NET 服务器 (IIS 7)The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Oracle11g温习-第十二章:tables

2013年4月27日 星期六 10:44 1、表的功能 存储、管理数据的基本单元(二维表:由行和列组成) 2、表的类型 1)普通表:【heap table(堆表) :数据存储时,无序的,对它的访问采用全表扫描】。 2)分区表:【(>2G) 对大表进行优化(Range Partitioning,List PartitioningHash P...

sql:关于Oracle的update from语句

      还是带有SQL Server的习惯,在Oracle中经常使用update from结构,事实上Oracle中是不存在from语句的。 Code 1      update hek_om_pop_lines_all 2      set quantity_2 = quantity_1 3      from hek_om_pop_lines_...

JS 的map和array集合组合返回JSON字符串

使用map 和array 返回自定义对象的JSON字符串: function getObjectJSON() {   var array = new Array();   for (var i = 0; i < 5; i++) {     var map = {};     map[1] = "张三";     map[2] = "李四";...

java日期转换

在java开发过程中,时间的转换时必须掌握的=========下面把时间转换做个总结,有可能不是很全面 时间格式只有两种 yyyy-MM-DD yyyy/MM/DD 时间的类型:字符串类型、sql类型、util类型、TimeStamp类型 1.Date转换成字符串时间 //Date 转换成字符串 Date date=new Date()...

JAVA获取某年(当年)的第一天的开始时刻和某年(当年)的最后一天的最后时刻

packagecom.date; importjava.text.SimpleDateFormat; importjava.util.Calendar; importjava.util.Date; public classTest { public static SimpleDateFormat format = new SimpleDat...

[ZZ] NumPy 处理数据

NumPy-快速处理数据--ndarray对象--数组的创建和存取https://www.cnblogs.com/moon1992/p/4946114.html NumPy-快速处理数据--ndarray对象--数组的创建和存取  本文摘自《用Python做科学计算》,版权归原作者所有。 NumPy为Python提供了快速的多维数组处理的能力,而SciP...