js_字符串对象
<!--@description-->
<!--@author beyondx-->
<!--@date Created in 2022/07/31/ 15:01-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>字符串对象</title>
</head>
<body>
<script>
// var s = 'kjawnz3bxrdzerxdbvf';
// console.log(s.length);
// 找到 字符d, 第一次出现的位置
// var n = s.indexOf('d');
// console.log(n);
/**
* 截取 字符串
* substr() 方法返回一个字符串中从 指定位置 开始到 指定字符数 的字符
*/
// var n = s.substr(1, 3);
// console.log(n); // expected output jaw
/**
* 字符串中的 大写, 全部转为 小写
* 字符串中的 小写, 全部转为 大写
*/
// var sc = "JavaScript";
// console.log(sc.toLowerCase()); // expected output "javascript"
// console.log(sc.toUpperCase()); // expected output "JAVASCRIPT"
/**
* 替换字符串 Java -> TypeScript
* 如果pattern是字符串,则仅替换第一个匹配项
* replace(pattern, replacement): replacement: 替换后的值
*/
// var sc = "JavaScriptJavaScript";
// console.log(sc.replace("Java", "Type")); // expected output: "TypeScriptTypeScript"
// // actural output: "TypeScriptJavaScript"
// // 仅替换 第1个
// const regex = /Java/i;
// console.log(sc.replace(regex, 'Type')); // acturally output: "TypeScriptJavaScript"
// // 替换所有, 全局替换; g: 全局
// const regex = /Java/g;
// console.log(sc.replace(regex, 'Type')); // acturally output: "TypeScriptTypeScript"
// const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
//
// console.log(p.replace('dog', 'monkey'));
// // expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
//
//
// const regex = /Dog/i;
// console.log(p.replace(regex, 'ferret'));
// // expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
// 删除字符串2边的 空白字符
var sentence = " javascript/typescript ";
console.log(sentence.length);
var newSentence = sentence.trim();
console.log(newSentence); // expected output: "javascript/typescript"
console.log(newSentence.length);
</script>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律