11 个你应该知道的JavaScript 字符串的基础知识
web前端开发
共 4998字,需浏览 10分钟
· 2022-10-15
英文 | https://medium.com/dailyjs/javascript-strings-10-fundamentals-you-should-know-d29e6e5f3a1a
"text"
'text'
"text" === 'text'
//true
"text with \"double quotes\" inside"
'text with "double quotes" inside'
"text with single 'quotes' "
"Text" === "Text"
//true
"The first line\nThe second line"
const str = "abc";
const newStr = str.replace("a", "A");
console.log(str);
//"abc"
console.log(newStr)
//"Abc"
const text = 'ABC';
text[0] = 'X';
//TypeError: Cannot assign to read only property '0' of string 'ABC'
new String(" text ").trim();
const text = " \t\ntext\n\t ";
const newText = text.trim();
//"text"
String(0)
//'0'
String(true)
//'true'
String(null)
//'null'
String(undefined)
//'undefined'
String([1,2,3])
//'1,2,3'
String({ msg: 'Hi'})
//'[object Object]'
String(Symbol('id'))
//'Symbol(id)'
"A" + " " + "text"
//"A text"
1 + "2"
//"12"
"A".concat("B")
//"AB"
"A".concat(" ", "text")
//"A text"
"1".concat(2)
//"12"
const quote = 'Winter is coming';
const words = quote.split(' ');
//["Winter", "is", "coming"]
const csv = 'Fire,and,Blood';
const arr = csv.split(',');
//["Fire", "and", "Blood"]
const arr = ['Fire', 'and', 'Blood'];
const text = arr.join(' ');
//'Fire and Blood'
const quote = "Here we stand";
const firstIndex = quote.indexOf(" ");
console.log(firstIndex) //4
const quote = "Here we stand";
const lastIndex = quote.lastIndexOf(" ");
console.log(lastIndex) //7
const quote = "First in Battle";
console.log(quote.startsWith("First"));
//true
const quote = "We Remember";
console.log(quote.endsWith("We"));
//false
const quote = "Our Blades are Sharp";
console.log(quote.includes("are"));
//true
const quote = "Winter is coming";
const part1 = quote.substr(0, 6);
//Winter
const part2 = quote.substr(10, 6);
//coming
const quote = "Winter is coming";
const part = quote.substr(6);
// is coming
const quote = "We Stand Together";
const part = quote.substring(3, 8);
// Stand
const quote = "We Stand Together";
const part = quote.substring(3);
// Stand Together
const quote = "You know nothing,Jon Snow";
const commaIndex = quote.indexOf(",");
const part = quote.substring(commaIndex + 1);
//"Jon Snow"
`Wisdom
and
Strength`
const word = "Awake";
`${word}! ${word}!`
//"Awake! Awake!"
const word = 'Hi';
console.log(word.length)
//2
const word = 'Hi🙂';
console.log(word.length)
//4
const word = 'Hi;
console.log(word.charAt(0))
//H
const word = 'Hi🙂';
console.log(word.charAt(2))
//�
const word = 'Hi🙂';
const chars = word.split("");
//["H", "i", "�", "�"]
const word = 'Hi🙂';
const chars = Array.from(word);
console.log(word.length);
//4
console.log(chars.length);
//3
console.log(chars[2]);
//🙂
学习更多技能
请点击下方公众号
评论
偷偷告诉你如何一台电脑开多个微信!
大家好,我是轩辕。前几天在粉丝群里,有人问我是怎么在一台电脑上同时登录两个微信的?正好之前写过一篇文章,分析过原理,分享给没看过的小伙伴学习一下。手机端多开微信估计很多人都知道,像华为、小米等手机系统都对此做了支持,不过在运行Windows系统的电脑上怎么启动两个微信呢?其实很简单,你只需要写一个批
编程技术宇宙
0
盘点Lombok的几个骚操作,你绝对没用过!
👉 欢迎加入小哈的星球 ,你将获得: 专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡 / 赠书福利全栈前后端分离博客项目 2.0 版本完结啦, 演示链接:http://116.62.199.48/ ,新项目正在酝酿中
小哈学Java
0
堪称最优秀的Docker可视化管理工具——Portainer你真的会用吗?
来源:blog.csdn.net/shark_chili3007/article/details/123366179👉 欢迎加入小哈的星球 ,你将获得: 专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡 / 赠书福利全栈前后端分离博客项目
小哈学Java
0
Apache Paimon毕业,湖仓架构的未来发展趋势!
北京时间 2024 年 4 月 16日,开源软件基金会 Apache Software Foundation(以下简称 ASF)正式宣布 Apache Paimon 毕业成为 Apache 顶级项目(TLP, Top Level Project)。经过社区的共同努力和持续创新,Apache Paim
程序源代码
0
JS的这些新特性,你都用过么?
大厂技术 高级前端 Node进阶点击上方 程序员成长指北,关注公众号回复1,加入高级Node交流群作为一门不断演进的语言,JavaScript每年都会引入新特性。这些特性的加入,能够帮助我们编写更加简洁、高效、易于维护的代码。然而,并非所有新特性
程序员成长指北
1
【深度学习】人人都能看懂的LSTM
熟悉深度学习的朋友知道,LSTM是一种RNN模型,可以方便地处理时间序列数据,在NLP等领域有广泛应用。在看了台大李宏毅教授的深度学习视频后,特别是介绍的第一部分RNN以及LSTM,整个人醍醐灌顶。本文就是对视频的记录加上了一些个人的思考。0. 从RNN说起循环神经网络(Recurrent Neur
机器学习初学者
0
我发现 Lombok的几个骚操作,哈哈好用
大家好,我是小富~前言本文不讨论对错,只讲骚操作。有的方法看看就好,知道可以这么用,但是否应用到实际开发中,那就仁者见仁,智者见智了。一万个读者就会有一万个哈姆雷特,希望这篇文章能够给您带来一些思考。耐心看完,你一定会有所收获。@onX例如 onConstructor, oMet
程序员内点事
0
测试新人,如何快速上手一个陌生的系统!
大家好,我是狂师!作为刚入行不久的测试新人,面对一个陌生的系统时,可能会感到有些手足无措。面对一个全新的系统系统,如何快速上手并展开有效的测试工作是一个重要的挑战。本文将探讨测试新人如何通过一系列步骤和策略,快速熟悉并掌握新系统的测试要点,从而提高测试效率和质量。本文旨在为测试新手提供一份指导,帮助
测试开发技术
0