JavaScript字符串方法终极指南-拆分
The split()
method separates an original string into an array of substrings, based on a separator
string that you pass as input. The original string is not altered by split()
.
split()
方法根据您作为输入传递的separator
字符串,将原始字符串分成子字符串数组。 原始字符串不会被split()
更改。
句法 (Syntax)
const splitStr = str.split(separator, limit);
separator
- a string indicating where each split should occurseparator
-一个字符串,指示每个拆分应该在哪里发生limit
- a number for the amount of splits to be foundlimit
-要找到的分割数量的数字
例子: (Examples:)
const str = "Hello. I am a string. You can separate me.";
const splitStr = str.split("."); // Will separate str on each period characterconsole.log(splitStr); // [ "Hello", " I am a string", " You can separate me", "" ]
console.log(str); // "Hello. I am a string. You can separate me."
Since we used the period (.
) as the separator
string, the strings in the output array do not contain the period in them – the output separated strings do not include the input separator
itself.
由于我们使用句点( .
)作为separator
字符串,因此输出数组中的字符串不包含句点-输出分隔的字符串不包含输入separator
本身。
You can operate on strings directly, without storing them as variables:
您可以直接对字符串进行操作,而无需将它们存储为变量:
"Hello... I am another string... keep on learning!".split("..."); // [ "Hello", " I am another string", " keep on learning!" ]
Also, string separator does not have to be a single character, it can be any combination of characters:
另外,字符串分隔符不必是单个字符,它可以是字符的任意组合:
const names = "Kratos- Atreus- Freya- Hela- Thor- Odin";
const namesArr = names.split("- "); // Notice that the separator is a dash and a space
const firstThreeNames = names.split("- ", 3);console.log(namesArr) // [ "Kratos", "Atreus", "Freya", "Hela", "Thor", "Odin" ]
console.log(firstThreeNames); // [ "Kratos", "Atreus", "Freya" ]
split
常见用途 (Common Uses of split
)
The split()
method is very useful once you grasp the basics. Here are a few common use cases for split()
:
一旦掌握了基础知识, split()
方法就非常有用。 这是split()
的一些常见用例:
根据句子创建单词数组: (Create an array of words from a sentence:)
const sentence = "Ladies and gentlemen we are floating in space.";
const words = sentence.split(" "); // Split the sentence on each space between wordsconsole.log(words); // [ "Ladies", "and", "gentlemen", "we", "are", "floating", "in", "space." ]
在单词中创建字母数组: (Create an array of letters in a word:)
const word = "space";
const letters = word.split("");console.log(letters); // [ "s", "p", "a", "c", "e" ]
反转单词中的字母: (Reversing the letters in a word:)
Because the split()
method returns an array, it can be combined with array methods like reverse()
and join()
:
因为split()
方法返回一个数组,所以它可以与诸如reverse()
和join()
类的数组方法结合使用:
const word = "float";
const reversedWord = word.split("").reverse().join("");console.log(reversedWord); // "taolf"
That's all you need to know to split()
strings with the best of 'em!
这就是您最好了解的用em split()
字符串的全部!
翻译自: https://www.freecodecamp.org/news/the-ultimate-guide-to-javascript-string-methods-split/
JavaScript字符串方法终极指南-拆分相关推荐
- JavaScript数组方法终极指南-地图
The map() method applies a function to each element in an array and returns a copy of the original a ...
- 12种降维方法终极指南(含Python代码)
12种降维方法终极指南(含Python代码) 你遇到过特征超过1000个的数据集吗?超过5万个的呢?我遇到过.降维是一个非常具有挑战性的任务,尤其是当你不知道该从哪里开始的时候.拥有这么多变量既是一个 ...
- javascript字符串方法总结
javascript中常用的字符串方法 String 的静态方法 fromCharCode:使用指定的Unicode值序列创建字符串 String.fromCharCode(num1, ..., nu ...
- JavaScript字符串方法——持续补充
字符串方法 1..length length 属性返回字符串的长度 var txt = "Hello"; txt.length; // 5 2..charAt(n) 返回字符串的第 ...
- JavaScript字符串方法汇总
字符串方法 String对象 ES5 字符串方法 seach() match() replace() ES6 字符串方法 repeat()重复输出字符串 starstWith()判断字符串是否以指定的 ...
- JavaScript字符串方法
一.创建字符串 创建字符串的三种办法: new String(), String(), 直接量,三种方式可以创建. String即文本(字符串),字符串方法都不改原字符串: length可读不可写. ...
- javascript字符串方法indexOf、lastIndexOf 方法的使用
indexOf.lastIndexOf语法 string.indexOf(searchvalue,start) string.lastIndexOf(searchvalue,start) 说明: st ...
- 12种降维方法终极指南
来源:Analytics Vidhya 编译:Bot 授权自 论智 你遇到过特征超过1000个的数据集吗?超过5万个的呢?我遇到过.降维是一个非常具有挑战性的任务,尤其是当你不知道该从哪里开始的时候. ...
- JavaScript字符串方法substr()截取前两个字符和后两个字符
substr()截取前两个字符 'abcd'.substr(0,2) substr()截取后两个字符 'abcd'.substr(-2,2) substr(par1,par2) 第一个参数是索引值,第 ...
最新文章
- ubuntu 局域网dns服务器_如何在 Ubuntu 16.04 服务器上配置内网 DNS 服务
- 2通过程序获得环境变量,getenv(),setenv()函数和unsetenv()函数,env查看环境变量,echo输出指定的环境变量
- 五、spring-data-Jpa 数据库操作
- 一些人一旦离开原来的单位,就不爱再去了,是为什么?
- HDU 6183 2017广西邀请赛:Color it(线段树)
- 海底捞张勇:谈钱,才是对员工最好的尊重
- mysql之我们终将踩过的坑(优化)
- MSDN下载出现链接格式有误,如何解决
- 简单的jsp代码(登录界面)
- 记Thinkpad的一次扩容升级经历
- mysql数据库的流水号生成,数据库流水号生成解决方案
- Pycahrm pip instell parsel时出现错误 error: Unable to find vcvarsall.bat解决过程
- 【中国数据创新琅琊榜】萨纳斯大数据实验室,高校大数据教学、实训、人才培养的最佳选择!
- SQLServer锁表
- OTA更新利用CRC保证程序的完整性
- 报表工具不可或缺的数据填报功能
- ssh远程登录输入yes说明
- 连接池以及连接池的使用
- SWUSTOJ #617 班级课程成绩计算
- Win10的Linux子系统Ubuntu换源
热门文章
- Java进阶之光!javaunicode码转字符
- 【面试总结】java测试工程师培训
- 大厂首发:338页网易Java面试真题解析火爆全网
- 第十二届湖南省赛 (B - 有向无环图 )(拓扑排序+思维)好题
- mysql数据库中case when 的用法
- 01-hibernate注解:类级别注解,@Entity,@Table,@Embeddable
- Asp.net mvc中使用配置Unity
- ThinkPHP 发送post请求
- uvalive 4973 Ardenia
- 一个java处理JSON格式数据的通用类(三)