博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript字符串方法终极指南-拆分
阅读量:2519 次
发布时间:2019-05-11

本文共 2792 字,大约阅读时间需要 9 分钟。

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 occur

    separator -一个字符串,指示每个拆分应该在哪里发生

  • limit - a number for the amount of splits to be found

    limit -要找到的分割数量的数字

例子: (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 spaceconst 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()字符串的全部!

翻译自:

转载地址:http://lqrwd.baihongyu.com/

你可能感兴趣的文章
fatal error C1853
查看>>
Ural 1001 - Reverse Root
查看>>
玩转webpack之webpack的entry output
查看>>
java 操作mongodb查询条件的常用设置
查看>>
黑马程序员_java基础笔记(02)...java语言基础组成
查看>>
关于缓存击穿
查看>>
对innodb 拷贝文件实现数据库的方式(转)
查看>>
python知识点 2014-07-09
查看>>
FloatingActionButton的一点学习感悟
查看>>
ABAP CDS ON HANA-(10)項目結合して一つ項目として表示
查看>>
网站地址信息
查看>>
产品经理 - 登录 注册
查看>>
小白的python进阶历程------05.占位符
查看>>
CF414BMashmokh and ACMDP
查看>>
Notepad++ 通过g++编译
查看>>
JAVA基础2——类初始化相关执行顺序
查看>>
转:Zend Framework 重定向方法(render, forward, redirect)
查看>>
Linux下查看磁盘与目录的容量——df、du
查看>>
关于日记app的思考
查看>>
使用sencha的cmd创建项目时提示找不到\Sencha\Cmd\repo\.sencha\codegen.json
查看>>