一天学一点点JS
hello 大家好!我是小夏,不爱唱歌的小夏。
新修的茅厕三天香,今天是第二天所以我的热情依旧不减,开始了第二天的更博。
今天很困,只睡了7小时不到,白天高强度学了一天。现在的小夏几乎是半睡状态,码字的手已经脱离大脑,大脑已经在梦海里云游。(这大概是4天前的草稿!)
今天学的是js字符串:
JS字符串
1. 什么是字符串
定义:字符串就是用单引号或者双引号包裹起来的,零个或多个排列在一起的字符。
字符串可以嵌套:
在单引号包裹的字符串内部,应该使用双引号进行嵌套。
在双引号包裹的字符串内部,应该使用单引号进行嵌套。
2. 字符串的使用
2.1换行:
字符串换行需使用反斜杠()
var x = "Hello \
World!";
2.2length属性
length:返回的是字符串的长度
var str ="hello world!";
console.log(str.length);
2.3字符索引
str[ ]方法:在字符串后面接中括号,中括号中写数字。能够访问到字符串中的每个字符。
ps:索引一次只能索引一个字符,如果需要多个则需要用+连接符。
var str ="hello world!";
console.log(str[0]+str[1]);
2.4获取字符串指定位置
1.charAt(index):返回的是具体的字符
2.charCodeAt(index)返回的是字符对应的Unicode编码(ascii编码值)
A:65 a:97 0:48
Index:就是字符串的位置(它是一个数字);
字符编码需要记得的两个。A-65,a-97。其他的英文字符累加就好。
3. fromCharCode将指定的数字(ascii码值)转为对应的字符
console.log(String.fromCharCode(65));
2.5字符串连接
**concat()**方法能够将两个字符串拼接起来,合成一个新的字符串。
可以认为concat和+的作用相同!
var str ='hello ',txt='小夏!';
console.log(str+txt+'你好帅!');
console.log(str.concat(txt,'你好帅!','酷哦!'));
2.6模板字符串
模板字符串(template string)是增强版的字符串,用反引号(`)标识。
模板字符串中嵌入变量,需要将变量名写在**${}**之中。
console.log(`${str}小胡誉`)
3、转义字符
常见的转义字符:
’ 单引号
" 双引号
\ 反斜杠
\n 换行
\r 光标到首行
\t tab(制表符)
4、字符串对象
javascript中有字符串类型string类型,我们也知道这种基本类型的变量的创建方式。
但javascript中还提供了另外一种字符串的声明方式,这种方式叫字符串对象。使用 new 关键字将字符串定义为一个对象
New String();
var str = new String('hello world!');
console.log(typeof(str));
5、字符串方法(重点!)
5.1、字符串方法
str.charAt(number):返回当前指定位置的字符;
var str=' hello world! ';
console.log(str.charAt(3));
str.charCodeAt(index):返回当前指定位置的字符ascII码值;
console.log(str.charCodeAt(3));
str.concat:连接字符串;
console.log(str.concat(str,'你好帅!'));
str.substring(start,end):截取字符串(从哪里开始到哪里结束,end:不包含end)
console.log(str.substring(0,5));
str.substr(start,length):截取字符串(从哪里开始取多长的字符)
console.log(str.substr(0,5));
.slice(start,end):截取字符串(end:不包含end)
console.log(str.slice(0,5));
.indexOf(str,offst):返回当前查找字符串在整个字符串中的首次位置,如果没有返回-1
Str:字符串
Offset:从哪里开始查找
console.log(str.indexOf('w'));
.lastIndexOf:倒过来查找
console.log(str.lastIndexOf('w'));
.trim():去掉字符串两端的空格
console.log(str)
console.log(str.trim())
.toUpperCase和toLowerCase:大小写转换
console.log(str.toUpperCase())
.match:返回一个指定字符串的数组
console.log(str.match('world'))
.search:返回位置
console.log(str.search('w'))
.replace:替换字符串
console.log(str.replace('w','x'))
.split:字符串切割,返回数组
console.log(str.split(''))
5.2、ES6新增的方法
· includes():返回布尔值,表示是否找到了参数字符串。
var str ='我是小夏!'
console.log(str.includes('小'))
· startsWith():返回布尔值,表示参数字符串是否在原字符串的头部。
console.log(str.startsWith('小',2))
· endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部。
console.log(str.endsWith('!'))
· 这三个方法都支持第二个参数,表示开始搜索的位置。
· repeat
方法返回一个新字符串,表示将原字符串重复n
次。
console.log(str.repeat(2))
ES2017 引入了字符串补全长度的功能。如果某个字符串不够指定长度,会在头部或尾部补全。padStart()
用于头部补全,padEnd()
用于尾部补全。
padStart()
和padEnd()
一共接受两个参数,第一个参数是字符串补全生效的最大长度,第二个参数是用来补全的字符串。
var str = '我是';
console.log(str.padStart(5,'你好!').padEnd(7,'小夏'))
字符串Base64编码
Base64本身是一种加密方式,可以将任意字符转成可打印字符。
有时需要以文本格式传递二进制数据,那么也可以使用 Base64 编码。
而我们使用这种编码方法,主要不是为了加密,而是为了不出现特殊字符,简化程序的处理。
javascript中字符串提供了两个有关Base64编码的方法:
btoa():字符串或二进制值转为Base64编码
atob():Base64编码转为原来的编码
var str ='I`m xiao xia!';
var base64=btoa(str);
console.log(atob(base64));
encodeURIComponent():要将非 ASCII 码字符转为 Base64 编码
decodeURIComponent():将转码后的内容转为非ASCII内容
var str ='我是小夏!';
var base64=encodeURIComponent(str);
console.log(base64);
console.log(decodeURIComponent(base64));
@author 小夏
终于码完了,小夏睡觉去咯!