码迷,mamicode.com
首页 > 编程语言 > 详细

定义字符串的方法和定义数组的方法

时间:2018-11-05 19:21:17      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:直接   san   zha   math   log   没有   结束   本质   hello   

  Object(基类)

  他下面的子类有:Function(函数)  Array(数组)  Number(数字)  Boolean(布尔)  String(字符串)  Date(日期)  Math(算术)  RegExp(正则)

  他们的本质都是函数

  String类

  定义字符串的方法

  var st=new String("hello world")    对象定义

  var st="hello world"         直接加引号

   字符串的+号运算     +仅仅就是拼接

  这种方法叫字符串的拼接(基础)

  字符串prototype的方法

  1)charAt()       返回指定位置的字符

  var st="hello world"

  var s=st.charAt(2)

  2)concat()      连接两个或多个字符串,并返回新的字符串

  var st="hello world"

  var sd="nihao"

  var s=st.concat(sd,"haha")

  3) indexOf()    (从前往后检索)返回某个字符串,在字符串中首次出现的位置  (如果没有那个字符返回 -1)

  var st="hello world"

  var sd="lo"

  console.log(st.indexOf(sd))

  4)lastIndexOf()   从后往前检索,返回某个字符串,在字符中首次出现的位置(如果没有那个字符返回 -1)

  和indexOf的写法,功能一样   

  5)match()

  6)search()

  7)replace()

  8)slice()     提取字符串中的一部分,并返回新的字符串

  var st="hello world"

  var s=st.slice(2,7)    (前闭后开,包含第二个字符,却不包含第七个字符)

  console.log(s)

  9)split()  将字符串以某种形式分割成数组

  var st="hello world hahaha 123 "

  var s=st.split(" ")

  console.log(s)

  10)  substr()   从指定位置,向后截取指定数字的字符串,并返回新的字符串

  var st="hello world"

  var s=st.substr(2,5)       从第三个字符开始(包括第三个字符)向后在数五个字符结束

  console.log(s)

  11)substring()     跟slice()一样

  12)toLowerCase()      将字符串中的字符都变成小写

  var st="HeLLo World"

  var s=st.toLowerCase(st)

  console.log(s)

  13)toUpperCase()      将字符串中的字符都变成大写

  var st="hello world"

  var s=st.toUpperCase(st)

  console.log(s)

  14)   trim()    指除去字符串中两端的空白

  var st="  hello world  "

  var s=st.trim(at)

  console.log(s)

  Array的方法

  数组的定义:var arr=new Array["12","zhangsan"]

        var arr=[12,"zhangsan"]      (建议大家都用简写的格式,这样别人觉得你牛逼)

  基本的方法

  1)push()   在数组的结尾添加一个新元素 (返回的值为数组的长度)

  var arr=["zhangsan","lisi","wangwu"]

  arr.push("zhaoliu")

  console.log(arr)

定义字符串的方法和定义数组的方法

标签:直接   san   zha   math   log   没有   结束   本质   hello   

原文地址:https://www.cnblogs.com/shangjun6/p/9910310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!