字符串的基本属性和方法

转义字符

  document.getElementsByTagName("P")[0].innerHTML = "my name is \'Stephen Curry\'"
  document.getElementsByTagName("P")[1].innerHTML = "my name is \"Stephen Curry\""
  document.getElementsByTagName("P")[2].innerText = "my name is \n Stephen Curry"
  document.getElementsByTagName("P")[3].innerText = "my name is \r Klay Thompson"
  document.write("my name is \\ Klay Thompson")
  // \t--tab制表符    \b--退格符   \f--换页符

字符串属性

  • length–返回字符串的长度
  • constructor–返回创建字符串属性的函数
 	const str = "这是一个字符串..."
    console.log(str.length); //返回字符串的长度---10
    console.log(str.constructor); //返回创建字符串属性的函数---ƒ String() { [native code] }
  • prototype–允许向对象添加属性和方法
function NBAPlayer(name, age, height) {
     this.name = name;
     this.age = age;
     this.height = height;
 }
  NBAPlayer.prototype.say = function() {
     console.log(`我是${this.name},今年${this.age},我的身高是${this.height}`) // 我是wangcai,今年30,我的身高是188
  }
 let p = new NBAPlayer("wangcai", 30, 188)
 NBAPlayer.prototype.salary = null; //允许您向对象添加属性和方法
 p.salary = 40000000
 console.log(p.salary); //40000000
 p.say()

字符串的方法

	var s = "Hello World"
    var s1 = "Hello"
    var s2 = "World"
    var num = 1205
    console.log(s.charAt(0)); //charAt() 返回指定索引位置的字符--H   
    console.log(s.charCodeAt(0)); //charCodeAt()返回指定索引位置字符的Unicode值--104
    console.log(s1.concat(s2)); //concat() 连接两个或多个字符串,返回连接后的字符串--HelloWorld
    console.log(String.fromCharCode(65)); //fromCharCode() 将 Unicode 转换为字符串--A
    console.log(s.indexOf("o")); //indexOf() 返回字符串中检索指定字符第一次出现的位置--4
    console.log(s.lastIndexOf("o")); //lastIndexOf() 返回字符串中检索指定字符最后一次出现的位置--7
    console.log(s1.localeCompare("s2")); //localeCompare() 用本地特定的顺序来比较两个字符串---(-1)不相等/0相等
    console.log(s.match("World")); //match() 找到一个或多个正则表达式的匹配--["World", index: 6, input: "Hello World", groups: undefined]
    console.log(s.replace(/\s+/g, ",")); // replace() 替换与正则表达式匹配的子串--Hello,World
    console.log(s.search("l")); //search() 检索与正则表达式相匹配的值--2
    console.log(s.slice(0, 5)); //slice() 提取字符串的片断, 并在新的字符串中返回被提取的部分--Hello
    console.log(s.split(",")); //split() 把字符串分割为子字符串数组--["Hello World"]
    console.log(s.substr(6, 5)); //substr() 从起始索引号提取字符串中指定数目的字符--World
    console.log(s.substring(6, 11)); //substring() 提取字符串中两个指定的索引号之间的字符--World
    console.log(s.toLocaleLowerCase()); //toLocaleLowerCase() 根据主机的语言环境把字符串转换为小写, 只有几种语言( 如土耳其语) 具有地方特有的大小写映射--hello world
    console.log(s.toLocaleUpperCase()); //toLocaleUpperCase() 根据主机的语言环境把字符串转换为大写, 只有几种语言( 如土耳其语) 具有地方特有的大小写映射--HELLO WORLD
    console.log(s.toLowerCase()); //toLowerCase()  把字符串转换为小写--hello world
    console.log(s.toUpperCase()); //toUpperCase() 把字符串转换为大写--HELLO WORLD
    console.log(typeof(num.toString())); //toString() 返回字符串对象值--string
    console.log(s.trim()); //trim() 移除字符串首尾空白--Hello World
    console.log(s.valueOf()); //valueOf() 返回某个字符串对象的原始值--Hello World
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

么心么肺

你的鼓励将是我学习的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值