-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.html
More file actions
53 lines (53 loc) · 2.68 KB
/
String.html
File metadata and controls
53 lines (53 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var str=' hello World ';
console.log(str.length);//字符串长度
console.log(str.charAt(4));//返回下标为4的子字符串
console.log(str.charCodeAt(4));//返回下标4的字符的ACSCII码
console.log(String.fromCharCode(97));//将ACSCII码转化成字符串
// 查找一个字符或者字符串在字符串中第一次出现的位置,未找到则返回-1
console.log(str.indexOf('a'));
console.log(str.indexOf('o'));
//倒序查找一个字符或者字符串在字符串中第一次出现的位置,返回位置,,未找到则返回-1
console.log(str.lastIndexOf('o'));
// 将指定的字符串替换,只能替换第一个
console.log(str.replace('h','a'));
//字符串截取,包含起始下标,不包含结束下标,识别负数
console.log(str.slice(0,5));
//字符串截取,不识别负数
console.log(str.substring(0,8));
//根据长度进行字符串截取, 1. 起始下标; 2. 截取的长度
console.log(str.substr(0,5));
//以某一字符串将目标字符串分割,返回值中传入的字符串会被删掉,不修改原字符串
console.log(str.split(''));
//将str字符串中的字符转化为小写
console.log(str.toLowerCase());
//将str字符串中的字符转化为大写
console.log(str.toLocaleUpperCase());
//将字符串左右空格去除
console.log(str.trim());
//在字符串内检索指定的值
console.log(str.match('e'));
//用于连接两个或多个字符串,不会修改原字符串
console.log(str.concat('a','b','c'));
// 向字符串开头(padStart)或结尾(padEnd)添加字符,,使字符串达到指定的长度。
//参数1: 当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度,则返回当前字符串本身。
//参数2: (可选)填充字符串。如果字符串太长,使填充后的字符串长度超过了目标长度,则只保留最左侧的部分,其他部分会被截断。默认用空格填充
console.log(str.padStart(20,'123'));
console.log(str.padEnd(20,'123'));
//获取时间
var time=new Date();
var h=String(time.getHours()).padStart(2,'0');
var m=String(time.getMinutes()).padStart(2,'0');
var s=String(time.getSeconds()).padStart(2,'0');
console.log(h,m,s);
</script>
</body>
</html>