日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

JavaScript字符串方法终极指南-拆分

發(fā)布時(shí)間:2023/11/29 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JavaScript字符串方法终极指南-拆分 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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()方法根據(jù)您作為輸入傳遞的separator字符串,將原始字符串分成子字符串?dāng)?shù)組。 原始字符串不會(huì)被split()更改。

句法 (Syntax)

const splitStr = str.split(separator, limit);
  • separator - a string indicating where each split should occur

    separator -一個(gè)字符串,指示每個(gè)拆分應(yīng)該在哪里發(fā)生

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

    limit -要找到的分割數(shù)量的數(shù)字

例子: (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.

由于我們使用句點(diǎn)( . )作為separator字符串,因此輸出數(shù)組中的字符串不包含句點(diǎn)-輸出分隔的字符串不包含輸入separator本身。

You can operate on strings directly, without storing them as variables:

您可以直接對(duì)字符串進(jìn)行操作,而無(wú)需將它們存儲(chǔ)為變量:

"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:

另外,字符串分隔符不必是單個(gè)字符,它可以是字符的任意組合:

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常見(jiàn)用途 (Common Uses of split)

The split() method is very useful once you grasp the basics. Here are a few common use cases for split():

一旦掌握了基礎(chǔ)知識(shí), split()方法就非常有用。 這是split()的一些常見(jiàn)用例:

根據(jù)句子創(chuàng)建單詞數(shù)組: (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." ]

在單詞中創(chuàng)建字母數(shù)組: (Create an array of letters in a word:)

const word = "space"; const letters = word.split("");console.log(letters); // [ "s", "p", "a", "c", "e" ]

反轉(zhuǎn)單詞中的字母: (Reversing the letters in a word:)

Because the split() method returns an array, it can be combined with array methods like reverse() and join():

因?yàn)閟plit()方法返回一個(gè)數(shù)組,所以它可以與諸如reverse()和join()類(lèi)的數(shù)組方法結(jié)合使用:

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/

總結(jié)

以上是生活随笔為你收集整理的JavaScript字符串方法终极指南-拆分的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。