<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>测试</title>
<meta name="viewport"
content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<script>
var str = "Hello ";
//显示长度
console.log(str.length);
//返回指定位置的字符
var result = str.charAt(0);
console.log(result);
//返回指定位置字符编码
result = str.charCodeAt(0);
console.log(result);
//利用字符编码创建一个字符
result = String.fromCharCode(72);
console.log(result);
//连接字符串
result = str.concat("World !");
console.log(result);
//检测是否含有某元素,返回第一次出现的位置,找不到返回-1;可以指定开始查找的起始位置
//lastindexOf()用法一样,从后查找
result = str.indexOf("o");
console.log(result);
result = str.indexOf("l", 0);
console.log(result);
result = str.indexOf("l", 3);
console.log(result);
//切片操作,从开始位置截取几个,省略第二个参数,则截取初始位置之后所有;可以为负数
str = "aaabbbcccdddeee";
result = str.slice(0, 3);
console.log(result);
//substring(),不接受负数
result = str.substring(0, 3);
console.log(result);
//拆分数组,拆分后 result 为数组,如果传递空串,则将所有字符拆分为数组元素
str = "abc,def,ghi";
result = str.split(",");
console.log(result);
//大小写转换
result = str.toUpperCase();
console.log(result);
result = str.toLowerCase();
console.log(result);
</script>
</head>
<body>
</body>
</html>
JavaScript学习之路——字符串操作
猜你喜欢
转载自blog.csdn.net/qq_41886231/article/details/100977166
今日推荐
周排行