老徐WEB:js入门学习 - javascript对象之String对象

简单说明一下字符串。


字符串是由单引号或者双引号括起来的任意文本。如果文本里含有单引号,外面就用双引号括起来,如果文本里含有双引号,那么外面就用单引号括起来。如果文本里面的引号和外面的一致,那么文本里的引号就要转义。


var txt = "Hello world";
var txt = 'don\'t warry';
var txt = new String("string");
字符串通过数字下标来获取字符。
var char = txt[3];


字符串对象属性


length - 字符串长度
prototype - String属性,可以添加属性和方法
constructor - 返回字符串构造函数


字符串对象函数


charAt() - 返回指定位置的字符,第一个字符从0开始。
var txt = "Hello world";
txt.charAt(0); // 返回H

charCodeAt() - 返回在指定的位置的字符的 Unicode 编码。
var txt = "Hello world";
txt.charCodeAt(0); // 返回72

concat() - 连接两个或更多字符串,并返回新的字符串。
var txt1 = 'abc';
var txt2 = 'def';
var txt3 = 'ghi';
txt1.concat(txt2); // abcdef
txt1.concat(txt2,txt3); // abcdefghi

fromCharCode() - 将 Unicode 编码转为字符。
String.fromCharCode(72); // H

indexOf() - 返回某个指定的字符串值在字符串中首次出现的位置。如果字符串中没有指定的字符,则返回-1.
var txt = 'Hello world';
txt.indexOf('world'); // 6
txt.indexOf('w'); // 6
txt.indexOf('a'); // -1

includes() - 查找字符串中是否包含指定的子字符串。若包含返回true,不包含返回false。
var txt = 'Hello world';
txt.includes('w'); // true
txt.includes('world'); // true
txt.includes('i'); // false

lastIndexOf() - 从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置。若没有搜索到字符,则返回-1。
var txt = 'Hello world';
txt.lastIndexOf('o'); // 7
txt.lastIndexOf('d'); // 10
txt.lastIndexOf('world'); // 6
txt.lastIndexOf('H'); // 0
txt.lastIndexOf('Hello'); // 0
txt.lastIndexOf('h'); // -1

match() - 查找找到一个或多个正则表达式的匹配。如果找到,返回一个数组,若没有找到,返回null。
var txt = 'Hello world';
txt.match(/Hello/g); // ["Hello"]
txt.match(/Hel/g); // ["Hel"]
txt.match(/l/g); // ["l","l","l"]
txt.match(/Helo/g); // null

repeat() - 复制字符串指定次数,并将它们连接在一起返回。
var txt = 'Hello';
txt.repeat(2); // HelloHello

replace() - 在字符串中查找匹配的子串, 并替换与正则表达式匹配的子串,返回替换后的结果。
var txt = 'Hello world';
txt.replace(/world/g,'world!'); // Hello world!

search() - 查找与正则表达式或字符串相匹配的起始位置。如果没有找到,则返回-1。
var txt = 'this is my book';
txt.search(/is/g); // 2
txt.search(/my/g); // 8
txt.search(/a/g); // -1

slice(start,end) - 提取字符串的片断,并在新的字符串中返回被提取的部分。不包含end位置的字符。
var txt = 'this is my book';
txt.slice(1,3); // his
txt.slice(1,6); // his i
txt.slice(-6,-1); // y boo

split() - 把字符串分割为字符串数组。
var txt = 'this is my book';
txt.split(' '); // ["this", "is", "my", "book"]

startsWith() - 查看字符串是否以指定的子字符串开头。如果是返回true,否则返回false。
var txt = 'Hello world';
txt.startsWith('Hello'); // true
txt.startsWith('H'); // true
txt.startsWith('h'); // false

substr(start,length) - 从起始索引号提取字符串中指定数目的字符。
var txt = 'Hello world';
txt.substr(1,3); // ell

substring(from,to) - 提取字符串中两个指定的索引号之间的字符。
var txt = 'Hello world';
txt.substring(3,8); // lo wo

toLowerCase() - 把字符串转换为小写。
var txt = 'Hello world';
txt.toLowerCase(); // hello world

toUpperCase() - 把字符串转换为大写。
var txt = 'Hello world';
txt.toUpperCase(); // HELLO WORLD

trim() - 去除字符串两边的空白
var txt = '  test ';
txt.trim(); // test

toLocaleLowerCase() - 根据本地主机的语言环境把字符串转换为小写。

toLocaleUpperCase() - 根据本地主机的语言环境把字符串转换为大写。

valueOf() - 返回某个字符串对象的原始值。

toString() - 返回一个字符串。

勤学苦练,笨鸟先飞。关注【老徐WEB前端开发教程】公众号,听老徐说。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值